leetcode 【 Subsets II 】python 实现
题目:
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 实现的更多相关文章
- [leetcode]Subsets II @ Python
原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...
- LeetCode Subsets II (DFS)
题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...
- [LeetCode] Subsets II 子集合之二
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [Leetcode] Subsets II
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [leetcode]Permutations II @ Python
原题地址:https://oj.leetcode.com/problems/permutations-ii/ 题意: Given a collection of numbers that might ...
- [leetcode]N-Queens II @ Python
原题地址:https://oj.leetcode.com/problems/n-queens-ii/ 题意:和N-Queens这道题其实是一样的,只不过这次要求返回的时N皇后的解的个数的问题. 解题思 ...
- [LeetCode] Subsets II [32]
题目 Given a collection of integers that might contain duplicates, S, return all possible subsets. Not ...
- [Leetcode] subsets ii 求数组所有的子集
Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...
- [LeetCode]Subsets II生成组合序列
class Solution {//生成全部[不反复]的组合.生成组合仅仅要採用递归,由序列从前往后遍历就可以. 至于去重,依据分析相应的递归树可知.同一个父节点出来的两个分支不能一样(即不能与前一个 ...
- LeetCode:Subsets I II
求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...
随机推荐
- java 创建一个新的http 请求的一种实现方式
项目中遇到要在后台向集群中的其他一台服务器发送一个请求,参考了网上一些材料,最终完成了需求.代码如下 /** * @Title requestURLWithPost * @Description:发送 ...
- sk-learning(2)
sk-learning 学习(2) sklearing 训练评估 针对kdd99数据集使用逻辑回归分类训练 然后进行评估 发觉分数有点高的离谱 取出10%数据494021条,并从中选择四分之一作为测试 ...
- window7防火墙无法更改某些设置,错误代码0×80070422
原因:这是由于管理工具的服务中的windows防火墙被禁用了. 解决方案:在window7中点击控制面板,然后点击管理工具,在点服务,然后找到windows firewall 然后将其改为自动就就可以 ...
- POJ-3436 ACM Computer Factory---最大流+拆点
题目链接: https://vjudge.net/problem/POJ-3436 题目大意: 每台电脑有p个组成部分,有n个工厂加工电脑.每个工厂对于进入工厂的半成品的每个组成部分都有要求,由p个数 ...
- 3204: 数组做函数参数--排序函数2--C语言
3204: 数组做函数参数--排序函数2--C语言 时间限制: 1 Sec 内存限制: 128 MB提交: 211 解决: 143[提交][状态][讨论版][命题人:smallgyy] 题目描述 ...
- 第26题:LeetCode572:Subtree of Another Tree另一个树的子树
题目描述 给定两个非空二叉树 s 和 t,检验 s 中是否包含和 t 具有相同结构和节点值的子树.s 的一个子树包括 s 的一个节点和这个节点的所有子孙.s 也可以看做它自身的一棵子树. 示例 1: ...
- linux下安装redis和使用
http://www.linuxidc.com/Linux/2014-05/101979.htm
- 三十一、MySQL 及 SQL 注入
MySQL 及 SQL 注入 如果您通过网页获取用户输入的数据并将其插入一个MySQL数据库,那么就有可能发生SQL注入安全的问题. 本章节将为大家介绍如何防止SQL注入,并通过脚本来过滤SQL中注入 ...
- ES6 的解构赋值前每次都创建一个对象吗?会加重 GC 的负担吗?
本文来源于知乎上的一个提问. 为了程序的易读性,我们会使用 ES6 的解构赋值: function f({a,b}){} f({a:1,b:2}); 这个例子的函数调用中,会真的产生一个对象吗?如果会 ...
- java util - 在java代码中执行javascript代码工具 rhino-1.7.7.jar
需要 rhino-1.7.7.jar 包 代码示例: package cn.java.mozilla.javascript; import org.mozilla.javascript.Context ...