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],
[]
]

Solution:

 public class Solution {
public List<List<Integer>> subsetsWithDup(int[] num) {
Arrays.sort(num);
List<List<Integer>> result = new ArrayList<List<Integer>>();
List<Integer> al = new ArrayList<Integer>();
dfs(result, al, num, 0);
return result;
} private void dfs(List<List<Integer>> result, List<Integer> al, int[] s,
int start) {
// TODO Auto-generated method stub
result.add(new ArrayList<Integer>(al));
for (int i = start; i < s.length; ++i) {
al.add(s[i]);
dfs(result, al, s, i+1);
al.remove(al.size()-1);
while((i+1<s.length)&&(s[i]==s[i+1]))
++i;
}
}
}

[Leetcode] Subsets II的更多相关文章

  1. LeetCode Subsets II (DFS)

    题意: 给一个集合,有n个可能相同的元素,求出所有的子集(包括空集,但是不能重复). 思路: 看这个就差不多了.LEETCODE SUBSETS (DFS) class Solution { publ ...

  2. [LeetCode] Subsets II 子集合之二

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  3. [leetcode]Subsets II @ Python

    原题地址:https://oj.leetcode.com/problems/subsets-ii/ 题意: Given a collection of integers that might cont ...

  4. [LeetCode] Subsets II [32]

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

  5. [Leetcode] subsets ii 求数组所有的子集

    Given a collection of integers that might contain duplicates, S, return all possible subsets. Note: ...

  6. [LeetCode]Subsets II生成组合序列

    class Solution {//生成全部[不反复]的组合.生成组合仅仅要採用递归,由序列从前往后遍历就可以. 至于去重,依据分析相应的递归树可知.同一个父节点出来的两个分支不能一样(即不能与前一个 ...

  7. LeetCode:Subsets I II

    求集合的所有子集问题 LeetCode:Subsets Given a set of distinct integers, S, return all possible subsets. Note: ...

  8. Subsets II - LeetCode

    目录 题目链接 注意点 解法 小结 题目链接 Subsets II - LeetCode 注意点 有重复的数字 数组可能是无序的,要先排序 解法 解法一:递归,只需要在Subsets中递归写法的基础上 ...

  9. LeetCode解题报告—— Word Search & Subsets II & Decode Ways

    1. Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be con ...

随机推荐

  1. 关于python装饰器(Decorators)最底层理解的一句话

    一个decorator只是一个带有一个函数作为参数并返回一个替换函数的闭包. http://www.xxx.com/html/2016/pythonhexinbiancheng_0718/1044.h ...

  2. hdu 1247:Hat’s Words(字典树,经典题)

    Hat’s Words Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total ...

  3. forEach 方法 (Array) (JavaScript)

    为数组中的每个元素执行指定操作. 语法 array1.forEach(callbackfn[, thisArg]) 参数 参数 定义 array1 必选.一个数组对象. callbackfn 必选.最 ...

  4. macosx安装MySQLdb

    折腾了半天,记录一下. 先按照这个步骤安装mysql-python 如果python setup.py install 时候出现clang 错误,运行 python -E setup.py insta ...

  5. 攻城狮在路上(叁)Linux(三十一)--- vim程序编辑器

    本篇主要介绍vim编辑器的使用方式,具体内容后续补充.

  6. 吐个槽,对VB6.0 还有VBS 说ByeBye

    往事不堪回首,折腾了个把月的老系统,心中郁结,不吐不快.系统架构是ASP +VBS +VB6.0 + SQL Server2000, 第一个版本开发完成大概是在2000年.基本是处于交接无力,看代码就 ...

  7. php mysql PDO基本操作

    <?php $dbh = new PDO('mysql:host=localhost;dbname=localhost', 'root', ''); $dbh->setAttribute( ...

  8. Build Instructions (Windows) – The Chromium Projects

    转自:http://121.199.54.6/wordpress/?p=1156 原始地址:http://www.chromium.org/developers/how-tos/build-instr ...

  9. 遍历List过程中删除元素的正确做法(转)

    遍历List过程中删除元素的正确做法   public class ListRemoveTest {     3 public static void main(String[] args) { 4 ...

  10. 智能车学习(十五)——K60野火2013版例程

    一.中断函数注册方法: 1.格式: 配置某个功能的中断 注册中断函数 开启中断 2.一个例子 pit_init_ms(PIT0,);//定时中断初始化 set_vector_handler(PIT0_ ...