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. 两个tomcat使用同一个jvm可能会出错

    如果两个tomcat中的项目的某些类具有完全相同的包路径和类名的话,jvm可能会“弄混”这两个类,所以一般要求包名“必须”唯一. 当然,如果两个类中的代码和import的类完全一样,弄混了也就弄混了, ...

  2. 自己做的板子,电脑识别不出下载口(micro-usb),程序无法烧入

    前几天画了一块cortex-A5的底板,出现一个问题,就是通过micro-usb与电脑连接之后,电脑无法识别出来,这就导致程序无法通过micro-usb烧入进去,经过仔细检查硬件,发现第一,元器件都没 ...

  3. 测试浏览器是否支持JavaScript脚本

    如果用户不能确定浏览器是否支持JavaScript脚本,那么可以应用HTML提供的注释符号进行验证.HTML注释符号是以 <-- 开始以 --> 结束的.如果在此注释符号内编写 JavaS ...

  4. Vue环境搭建-项目的创建-启动生命周期-组件的封装及应用

    vue项目环境的搭建 """ node >>> python:node是用c++编写用来运行js代码的 npm(cnpm) >>> p ...

  5. 关于XML的一些总结

    xml的知识结构图 eXtensible Markup Language,可扩展标记语言,简称XML,和HTML比较而言,语法相似,作用不同 XML被设计用来存储和传输数据,但存储数据方面,数据库是主 ...

  6. String Distance and Transform Process

    http://acm.hdu.edu.cn/showproblem.php?pid=1516 Problem Description String Distance is a non-negative ...

  7. 九、linux-msyql下的mysql主从复制深度实战

    1.上节基本诉说了mysql主从同步,这里想说明的是,其一从库在请求主库进行同步的时候,是主库的主线程进行用户名.密码的验证,在验证通过后,将请求转交给I/O线程负责同步:其二从库sql线程在读取中继 ...

  8. 108)PHP分页显示

    一个代码页的链接:https://www.cnblogs.com/mmykdbc/p/6688460.html 首先一个简单的代码展示: 目录关系: 数据库表格展示: 结果展示:   然后  代码展示 ...

  9. 42)PHP,mysqli函数功能总结

    fetch----------------一个一个的取值,这个注意 fetch_array(),fetch_assoc(),fetch_object(),这三个方法的使用请看手册 请注意是FETCH, ...

  10. Windows下的GUI 库

    Windows 下的 GUI 解决方案比较多: 基于 C++ 的有 Qt.MFC.WTL.wxWidgets.DirectUI.Htmlayout: 基于 C# 的有 WinForm.WPF: 基于 ...