题目

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差不多,详情见:

http://www.cnblogs.com/xbf9xbf/p/4253208.html

只需要每次向result中添加子集时注意判断一下这个子集是否已经存在。如果存在那么就直接返回,不做处理。

leetcode 【 Subsets II 】python 实现的更多相关文章

  1. [leetcode]Subsets II @ Python

    原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...

  2. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  3. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  4. [Leetcode] Subsets II

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  5. [leetcode]Permutations II @ Python

    原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...

  6. [leetcode]N-Queens II @ Python

    原题地址:https://oj.leetcode.com/problems/n-queens-ii/ 题意:和N-Queens这道题其实是一样的,只不过这次要求返回的时N皇后的解的个数的问题. 解题思 ...

  7. [LeetCode] Subsets II [32]

    题目 Given a collection of integers that might contain duplicates, S, return all possible subsets. Not ...

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

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  9. [LeetCode]Subsets II生成组合序列

    class Solution {//生成全部[不反复]的组合.生成组合仅仅要採用递归,由序列从前往后遍历就可以. 至于去重,依据分析相应的递归树可知.同一个父节点出来的两个分支不能一样(即不能与前一个 ...

  10. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

随机推荐

  1. Python学习笔记-day1(while流程控制)

    count = 0 while True: #print('count:',count) if count == 3: print('you guess over 3 times!fuck off!' ...

  2. CSS变量(CSS variable)

    使用 CSS 变量编写你的样式代码 基本使用: 1. --variable: <declaration-value> 2. <css-attribute>: var(--var ...

  3. Javascript 向量

    向量 既有大小又有方向的量叫做向量(亦称矢量),与标量相对,用JS实现代码如下,直接搬miloyip的了 Vector2 = function(x, y) { this.x = x; this.y = ...

  4. 优化通过redis实现的一个抢红包流程【下】

    上一篇文章通过redis实现的抢红包通过测试发现有严重的阻塞的问题,抢到红包的用户很快就能得到反馈,不能抢到红包的用户很久(10秒以上)都无法获得抢红包结果,起主要原因是: 1.用了分布式锁,导致所有 ...

  5. pycharm 安装插件 支持markdown

    github项目中的README文件通常是md格式的,但是pycharm默认是不支持的,需要安装插件 进入settings中搜索plugins,然后在plugins中搜索markdown suppor ...

  6. java调用摄像头

    http://blog.csdn.net/xing_sky/article/details/43482213 原文地址:http://blog.csdn.net/zajin/article/detai ...

  7. C#进阶之全面解析Lambda表达式

    引言 在实际的项目中遇到一个问题,我们经常在网上搜索复制粘贴,其中有些代码看着非常的简洁,比如Lambda表达式,但是一直没有去深入了解它的由来,以及具体的使用方法,所以在使用的时候比较模糊,其次,编 ...

  8. spring中@Autowrite注解和@Resource的区别

    spring不但支持自己定义的@Autowired注解,还支持几个由JSR-250规范定义的注解,它们分别是@Resource.@PostConstruct以及@PreDestroy. @Resour ...

  9. c++ 作业 10月13日 进制转换最简单方法,控制c++输出格式方法 教材50的表格自己实践一下 例题3.1 setfill() setw()

    #include <iostream> #include <iomanip> using namespace std; int main(){ // int i; // cou ...

  10. 记录JQ-WEUI中滚动加载的一个BUG

    最近写微信公众号,用到的技术栈是jq+vue的混合开发,采用的UI是移动端比较火的WEUI,在微信开发中应该较广泛.个人看惯了elementUI文档,相对于饿了么组件文档的详细,WEUI的文档还是比较 ...