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. Maven--Maven 入门

    1.POM <?xml version="1.0" encoding="utf-8" ?> <project xmlns="http ...

  2. Linux-进程关系

    (1).无关系 (2).父子进程关系 (3).进程组(group):由若干个进程构成一个进程组 (4).会话(session):由若干个进程组构成一个会话

  3. Codeforces1303F Number of Components

    Description link 题意:给一个全\(0\)矩阵,每次支持一个修改,修改不还原(这要是还原了不就成\(A\)题了) 然后询问每一次修改完了当前矩阵的连通块个数 每一个修改的值单调不降 修 ...

  4. springmvc 实现文件上传

    1.添加jar包 <dependency> <groupId>commons-io</groupId> <artifactId>commons-io&l ...

  5. Different Gene Frequencies in the Two Sexes

    I.7 Different Gene Frequencies in the Two Sexes 假设存在一种基因仅在第一代亲代的不同性别之间的概率有区别,比如,A 在male中频率是Pm,a是(1-P ...

  6. DocCms_2016 代码审计

    0x00 结构浏览 按照代码审计的惯例,拿到这款cms之后首先浏览了一下目录结构,在基本了解之后,首先进入/index.php,这里包含了两个文件:/admini/config/qd-config.p ...

  7. linux下特殊命令集锦

    1.ifconfig -a | grep enp0 | cut -d : -f 1   //按照:进行切割网络文件名 如:ifconfig `ifconfig -a | grep enp0 | cut ...

  8. 每天学点linux命令

    用于创建LVM卷组 补充说明 vgcreate命令 用于创建LVM卷组.卷组(Volume Group)将多个物理卷组织成一个整体,屏蔽了底层物理卷细节.在卷组上创建逻辑卷时不用考虑具体的物理卷信息. ...

  9. MRP执行计划列表(禁用)

    1.最直接的方法,推进方法 2.比较麻烦的方法

  10. Struts配置文件以Spring的方式实现自定义加载

    在使用struts时,我们需要在web.xml中配置过滤器,同时我们需要配置struts的配置文件路径来加载项目中struts的相关配置信息.如果我们不配置路径的话,Struts会有一些默认的加载路径 ...