[Leetcode]@python 90. Subsets II.py
生活随笔
收集整理的這篇文章主要介紹了
[Leetcode]@python 90. Subsets II.py
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
題目鏈接
https://leetcode.com/problems/subsets-ii/
題目原文
Given a collection of integers that might contain duplicates, nums, return all possible subsets.
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If nums = [1,2,2], a solution is:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]]
題目大意
給出一個(gè)有重復(fù)元素的所有集合的子集的list
解題思路
使用dfs進(jìn)行求解
代碼
class Solution(object):def subsetsWithDup(self, nums):""":type nums: List[int]:rtype: List[List[int]]"""def dfs(depth, start, valuelist):if valuelist not in ans:ans.append(valuelist)if depth == len(nums):returnfor i in range(start, len(nums)):dfs(depth + 1, i + 1, valuelist + [nums[i]])nums.sort()ans = []dfs(0, 0, [])return ans轉(zhuǎn)載于:https://www.cnblogs.com/slurm/p/5206242.html
總結(jié)
以上是生活随笔為你收集整理的[Leetcode]@python 90. Subsets II.py的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 读《About Face 4 交互设计精
- 下一篇: 《FLUENT 14流场分析自学手册》—