python solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class (object):
def subsetsWithDup(self, nums):
"""
:type nums: List[int]
:rtype: List[List[int]]
"""
"""
思路整理:DFS recursion
1)对nums进行排序以避免nums中重复元素不聚集在一起如[1,2,4,4,3,4]
2)从nums中依次取出元素加到path中进行DFS
3)对于重复元素如[1,2,2]中的2,res中每个以2为开头的数组的只取nums的第一个2
Explanation
1)sort nums to gather all same number together
2)take numbers from nums iteratively and use them to conduct DFS
3)for same numbers like 2 in [1,2,2],each array only take the first 2 as its
first 2
"""
def helper(start,end,path,res):
res.append(path)
for i in range(start,end):
if i!=start and nums[i]==nums[i-1]:continue
helper(i+1,end,path+[nums[i]],res)
return
res=[]
nums.sort()
helper(0,len(nums),[],res)
return res大专栏  LeetCode Problem 90. Subsets IIbr/>

java solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
对于无重复元素的n维数组 有2^n个子数组 因为每个元素在每个子数组里都只有出现或者不出现两种可能
而对于有重复元素的数组如[1,2,2]
(1)[]
(2)[] [1] 复制一份[] 并向其中加入1
(3)[] [1] [2] [1,2] 复制一份[] [1] 并向其中加入2
(3)[] [1] [2] [1,2] [2,2] [1,2,2]对于重复元素 只向前一次添加过与其相同元素的数组中添加该元素
复制一份[2] [1,2] 并向其中加入2
*/
class {
public List<List<Integer>> subsetsWithDup(int[] nums) {
List<List<Integer>> res=new ArrayList<List<Integer>>();
int start=0;
res.add(new ArrayList<Integer>());
Arrays.sort(nums);
for(int i=0;i<nums.length;i++)
{
if(i==0||nums[i]!=nums[i-1]) start=0;
int size=res.size();
for(int j=start;j<size;j++)
{
List<Integer> temp=new ArrayList<Integer>(res.get(j));
temp.add(nums[i]);
res.add(temp);
}
start=size;
}
return res;
}
}

LeetCode Problem 90. Subsets II的更多相关文章

  1. 【一天一道LeetCode】#90. Subsets II

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given a ...

  2. 【LeetCode】90. Subsets II (2 solutions)

    Subsets II Given a collection of integers that might contain duplicates, S, return all possible subs ...

  3. 【LeetCode】90.Subsets II

    Subsets II Given a collection of integers that might contain duplicates, nums, return all possible s ...

  4. 【LeetCode】90. Subsets II 解题报告(Python & C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 回溯法 日期 题目地址:https://leet ...

  5. leetcode 78. Subsets 、90. Subsets II

    第一题是输入数组的数值不相同,第二题是输入数组的数值有相同的值,第二题在第一题的基础上需要过滤掉那些相同的数值. level代表的是需要进行选择的数值的位置. 78. Subsets 错误解法: cl ...

  6. 78. Subsets(M) & 90. Subsets II(M) & 131. Palindrome Partitioning

    78. Subsets Given a set of distinct integers, nums, return all possible subsets. Note: The solution ...

  7. 90. Subsets II

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

  8. LeetCode 90. Subsets II (子集合之二)

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

  9. [LeetCode] 90.Subsets II tag: backtracking

    Given a collection of integers that might contain duplicates, nums, return all possible subsets (the ...

随机推荐

  1. 试验指标|试验单位|均方|随机模型|固定模型|字母标记法|LSR|q检验|LSD|重复值|弥补缺失数据|可加性|平方根转换|对数转换|反正弦转化

    第五章 方差分析 试验指标是什么? 就是统计的测量值,eg:身高体重 试验单位( experimental unit )是什么? 实验载体,比如一只小白鼠 均方是什么? 就是方差 随机模型的τ有何特点 ...

  2. ORBSLAM2的资源

    ORBSLAM2代码总结 https://blog.csdn.net/hzwwpgmwy/article/details/82462988 ORBSLAM2局部地图更新实现 https://blog. ...

  3. c#为什么要用事物

    一.事务的定义 所谓事务,它是一个操作集合,这些操作要么都执行,要么都不执行,它是一个不可分割的工作单位.典型的例子就像从网上银行系统的帐户A转帐到帐户B,它经过两个阶段:1.从帐户A取出款项.2.把 ...

  4. jQuery篇

    jQuery 1.为什么使用jQuery? js中window onload事件只能出现一次,如果出现多次,后面的事件会覆盖掉前面的事件 js代码容错差 简单的动画效果实现很繁琐,例如简单的动画渐变效 ...

  5. 指定linux 下默认python版本

    sudo rm -rf python sudo ln -s /usr/bin/python3 /usr/bin/python

  6. Opencv笔记(四)——绘图函数

    常用的绘图函数有: cv2.line()       cv2.circle()        cv2.rectangle()      cv2.ellipse()       cv2.putText( ...

  7. MFC的程序,不想显示窗口,任务栏里也不显示

    在dialog的oninitdialog里设置如下属性,很简单,网上一些乱七八糟的做法,一行代码就能搞定啊 SetWindowPos(&CWnd::wndNoTopMost,0,0,0,0,S ...

  8. 使用idea创建spring mvc项目图文教程

    使用idea创建spring mvc项目图文教程 前言: 使用惯了eclipse的朋友,如果刚换成了idea或许有些不习惯.但是使用idea之后,就会love上idea了.本文将通过图文讲解怎么通过i ...

  9. Android activity 亮度调整

    注意点 screenBrightness 取值范围0-1 不是0-255一定要注意 scanForActivity(context) 是根据上下文获取所在的activity如果直接在activity ...

  10. 80)PHP,扩展工具类

    啥是扩展工具类:这个问题很深奥,自己慢慢理解吧. 首先  对于session的处理函数是扩展工具类. ②图片处理类 ③验证码生成类 ④算是项目中的一个功能模块. 扩展工具类   放在我们的framew ...