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

Note: The solution set must not contain duplicate subsets.

Example:

Input: [1,2,2]
Output:
[
[2],
[1],
[1,2,2],
[2,2],
[1,2],
[]
]
 
class Solution {
public List<List<Integer>> subsetsWithDup(int[] nums) {
result = new ArrayList<>();
List<Integer> ans = new ArrayList<Integer>();
Arrays.sort(nums); //nums可能是乱序的,要先排序
backtrack(ans, nums, 0);
return result;
} public void backtrack(List<Integer> ans, int[] nums, int depth){
if(depth >= nums.length) {
List<Integer> new_ans = new ArrayList<Integer>(ans);
result.add(new_ans);
return;
} int i = depth+1;
while(i < nums.length){
if(nums[depth] == nums[i]) i++;
else break;
} int j = depth;
backtrack(ans, nums, i); //not add
while(j < i){
ans.add(nums[depth]);
backtrack(ans, nums, i);
j++;
} //reset
while(j > depth){
ans.remove(ans.size()-1);
j--;
}
} private List<List<Integer>> result;
}

90. Subsets II (Java)的更多相关文章

  1. leetcode 78. Subsets 、90. Subsets II

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

  2. LeetCode Problem 90. Subsets II

    python solution 123456789101112131415161718192021222324252627 class (object): def subsetsWithDup(sel ...

  3. 90. Subsets II

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

  4. 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 ...

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

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

  6. 【LeetCode】90.Subsets II

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

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

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

  8. 78. Subsets 90. Subsets II

    1. Given a set of distinct integers, nums, return all possible subsets. Note: Elements in a subset m ...

  9. 90. Subsets II (Back-Track, DP)

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

随机推荐

  1. LC 650. 2 Keys Keyboard

    Initially on a notepad only one character 'A' is present. You can perform two operations on this not ...

  2. 自定义ViewPager+RadioGroup联动效果的实现

    package com.loaderman.myviewpager; import android.os.Bundle; import android.support.v7.app.AppCompat ...

  3. GMM-EM实验结果

  4. Delphi组件编辑器

    看到Dev中的cxGrid组件的编辑器很强大,于是很想探究一下,跟踪cxGrid的代码比较麻烦,但原理大概知道一二.首先来研究一下设计器双击cxGrid弹出一个编辑窗体,选择窗体中的一个内容后,属性编 ...

  5. IPV6测试方法

    终端 dig +nocmd + nostats 你的域名 AAAA: 查看Got answer 如果 status的状态是NO ERROR 那就是支持IPV6 就没啥问题. 如果status 的状态是 ...

  6. C# 线程thread

    一.问题总结 1. 在WinForm开发过程中用到线程时,往往需要在线程中访问线程外的控件,比如:设置textbox的Text值等等.如果直接访问UI控件会报出“从不是创建控件的线程访问它”错误.控件 ...

  7. python-Web-django-自定义标签

    简化:@register.simple_tag def current_time(token): return datetime.datetime.now().strftime(str(token)) ...

  8. 华为HCNA乱学Round 4:RIP

  9. 【Linux开发】linux设备驱动归纳总结(一):内核的相关基础概念

    linux设备驱动归纳总结(一):内核的相关基础概念 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ...

  10. PTA(Basic Level)1022.D进制的A+B

    输入两个非负 10 进制整数 A 和 B (≤230−1),输出 A+B 的 D (1<D≤10)进制数. 输入格式: 输入在一行中依次给出 3 个整数 A.B 和 D. 输出格式: 输出 A+ ...