leetcode 【 Subsets II 】python 实现
2015-01-27 19:48
281 查看
题目:
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 =
代码:oj测试通过 Runtime: 78 ms
思路:
大体思路跟Subsets差不多,详情见:
/article/5261266.html
只需要每次向result中添加子集时注意判断一下这个子集是否已经存在。如果存在那么就直接返回,不做处理。
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], [] ]
代码:oj测试通过 Runtime: 78 ms
class Solution: # @param num, a list of integer # @return a list of lists of integer def dfs(self, start, S, result, father_elements): if father_elements in result: return result.append(father_elements) for i in range(start, len(S)): self.dfs(i+1, S, result, father_elements+[S[i]]) def subsetsWithDup(self, S): # none case if S is None: return [] # deep first search result = [] first_depth = {} self.dfs(0, sorted(S), result, []) return result
思路:
大体思路跟Subsets差不多,详情见:
/article/5261266.html
只需要每次向result中添加子集时注意判断一下这个子集是否已经存在。如果存在那么就直接返回,不做处理。
相关文章推荐
- [LeetCode]3 Longest Substring Without Repeating Characters (C++,Python实现)
- [LeetCode]5 Longest Palindromic Substring(C++,Python实现)
- leetcode 【 Sort List 】 python 实现
- leetcode 【 Partition List 】python 实现
- leetcode 【 Swap Nodes in Pairs 】python 实现
- [LeetCode]1 Two Sum(C++,Python实现)
- leetcode 【 Add Two Numbers 】 python 实现
- leetcode 【 Linked List Cycle II 】 python 实现
- leetcode 【 Reverse Words in a String 】python 实现
- leetcode 【 Copy List with Random Pointer 】 python 实现
- LeetCode 2. Add Two Numbers (C++/python/js实现)
- leetcode 【 Remove Nth Node From End of List 】 python 实现
- leetcode 【 Linked List Swap Nodes in Pairs 】 python 实现
- leetcode 【 Insertion Sort List 】 python 实现
- leetcode 【 Linked List Cycle 】 python 实现
- [LeetCode]4 Add Two Numbers(C++,Python实现)
- 【LeetCode with Python】 Subsets II
- leetcode 【 Remove Duplicates from Sorted List II 】 python 实现
- leetcode 【 Reverse Nodes in k-Group 】 python 实现
- leetcode 【 Convert Sorted List to Binary Search Tree 】python 实现