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. 01.Java安装及环境变量的设置

    1.下载 https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.mac上安装及配 ...

  2. Oauth2.0详解及安全使用

    引言:刚刚参加工作的时候接到的第一个任务就是接入新浪的联合登录功能,当时新浪用的还是oauth1.0协议.接入的时候没有对oauth协议有过多的了解,只是按照开放平台的接入流程进行开发,当时还在想这么 ...

  3. SQL:找到特定日期每个顾客最高购买量:Find the highest purchase amount ordered by the each customer on a particular date, with their ID, order date and highest purchase amount.

    A: SELECT customer_id,ord_date,MAX(purch_amt) FROM orders GROUP BY customer_id,ord_date; find the hi ...

  4. HTTP Error 502.5 - Process Failure 解决方案

    .netcore 2.1.4的程序部署到IIS后报以下错误: ======================================================= HTTP Error 50 ...

  5. Python基础学习五

    Python基础学习五 迭代 for x in 变量: 其中变量可以是字符串.列表.字典.集合. 当迭代字典时,通过字典的内置函数value()可以迭代出值:通过字典的内置函数items()可以迭代出 ...

  6. 日期控件 My97DatePicker WdatePicker 日期格式

    WdatePicker()只显示日期 WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss'})显示日期和时间 WdatePicker({dateFmt:'yyyy-MM ...

  7. 实现hashmap

    /**数组下面挂着链表*/ #include<stdio.h> #include<unistd.h> #include<stdlib.h> #include< ...

  8. Flume(二) —— 自定义拦截器、Source、Sink

    自定义拦截器 自定义Source 自定义Sink 引入依赖 <dependency> <groupId>org.apache.flume</groupId> < ...

  9. MySQL数据库高可用集群搭建-PXC集群部署

    Percona XtraDB Cluster(下文简称PXC集群)提供了MySQL高可用的一种实现方法.集群是有节点组成的,推荐配置至少3个节点,但是也可以运行在2个节点上. PXC原理描述: 分布式 ...

  10. 吴裕雄--天生自然python学习笔记:python 用pygame模块游戏开发

    游戏开发在软件开发领域占据了非常重要的位直.游 戏开发需要用到的技术相当广泛,除了多媒体.图片.动 画的处理外,程序设计更是游戏开发的核心内容. Py game 是为了让 Python 能够进行游戏开 ...