[leetcode]Subsets II @ Python

原题地址:https://oj.leetcode.com/problems/subsets-ii/

题意:

Given a collection of integers that might contain
duplicates, S, 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 S = [1,2,2],
a solution is:

[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]

解题思路:和上一道题一样,求一个集合的所有子集。和上一道题不一样的一点是集合可能有重复元素。这道题同样使用dfs来解题,只是需要在dfs函数里加一个剪枝的条件,排除掉同样的子集。

代码:


class Solution:
# @param num, a list of integer
# @return a list of lists of integer
def subsetsWithDup(self, S):
def dfs(depth, start, valuelist):
if valuelist not in res: res.append(valuelist)
if depth == len(S): return
for i in range(start, len(S)):
dfs(depth+1, i+1, valuelist+[S[i]])
S.sort()
res = []
dfs(0, 0, [])
return res

[leetcode]Subsets II @ Python,布布扣,bubuko.com

时间: 2024-10-11 12:03:08

[leetcode]Subsets II @ Python的相关文章

LeetCode: Subsets II [091]

[题目] Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2], a solutio

[LeetCode] Subsets II [32]

题目 Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2], a solution

LeetCode Subsets II (DFS)

题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) 1 class Solution { 2 public: 3 vector<vector<int>> subsets(vector<int>& nums) { 4 sort(nums.begin(),nums.end()); 5 DFS(0,nums,tmp); 6 ans.push_back(vector

LeetCode Subsets II

Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2], a solution is:

[Leetcode] subsets ii 求数组所有的子集

Given a collection of integers that might contain duplicates, S, 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 S =[1,2,2], a solution is: [

[leetcode]N-Queens II @ Python

原题地址:https://oj.leetcode.com/problems/n-queens-ii/ 题意:和N-Queens这道题其实是一样的,只不过这次要求返回的时N皇后的解的个数的问题. 解题思路:上道题使用了递归回溯的解法,这道题我们可以使用非递归回溯来解决,因为如果使用递归回溯来解决,那么代码和上道题几乎一样.在非递归的编程中,比较有技巧性的是如何来进行回溯. 代码: class Solution: # @return an integer def totalNQueens(self,

[LeetCode]Subsets II生成组合序列

class Solution {//生成所有[不重复]的组合.生成组合只要采用递归,由序列从前往后遍历即可.至于去重,根据分析对应的递归树可知,同一个父节点出来的两个分支不能一样(即不能与前一个元素一样,且前一个元素要与之在同层). public: int *b,n; vector<int>a; vector<vector<int> >ans; void dfs(int id,int len){ if(len>0){ vector<int>v(b,b+

LeetCode --- 90. Subsets II

题目链接:Subsets II Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2]

leetcode: Subsets &amp; Subsets II

SubsetsGiven a set of distinct integers, S, 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 S = [1,2,3], a solution is: [ [3], [1], [2], [1,2