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. 微信小程序之阻止冒泡事件

    众所周知,在微信小程序给标签绑定点击方法大家都会想到 "bindtap" 但是在页面中会遇到 点击 会冒泡而触发其他元素的时间发生 那么怎么办呢 就把引发冒泡事件的始作俑者的 bi ...

  2. c#根据配置文件反射

    由于项目中用到了反射,准备把各个类库都先写在配置文件中,然后读取配置文件,再对配置文件中配置的类库进行反射. 这样做的好处是各个类库保持独立,其中一个类库出现问题不会影响其他类库,更新项目时,只要更新 ...

  3. C基础知识(6):指针--函数指针与回调涵数

    函数指针 函数指针是指向函数的指针变量. 通常我们说的指针变量是指向一个整型.字符型或数组等变量,而函数指针是指向函数. 函数指针可以像一般函数一样,用于调用函数.传递参数. #include < ...

  4. centOS7忘记密码,修改root账号密码

    centOS7忘记密码,修改root账号密码 RHEL7 的世界发生了变化,重置 root 密码的方式也一样.虽然中断引导过程的旧方法(init=/bin/bash)仍然有效,但它不再是推荐的.“Sy ...

  5. Python浮点型数据小数点的取舍

    python默认的是17位小数的精度 1.round()内置方法 π=3.1415926535 new_num=round(π,2)     #四舍五入保留两位小数 print(new_num)    ...

  6. CENTOS7下安装和配置MYSQL问题记录

    1.安装 下载mysql源安装包 shell> wget http://dev.mysql.com/get/mysql57-community-release-el7-8.noarch.rpm ...

  7. PTA --- 天梯赛 L1-059 敲笨钟

    L1-059 敲笨钟 (20 point(s)) 微博上有个自称“大笨钟V”的家伙,每天敲钟催促码农们爱惜身体早点睡觉.为了增加敲钟的趣味性,还会糟改几句古诗词.其糟改的方法为:去网上搜寻压“ong” ...

  8. 使用URLOS在linux系统中极速部署NFS共享存储服务

    如何在linux系统里搭建NFS服务?其实我们只需要安装一个URLOS面板,然后就能在3分钟内将NFS服务部署完成.近日,URLOS在应用市场中上架了一款NFS应用,它可以让我们的节点主机在3分钟内极 ...

  9. mysql——插入、更新、删除数据(概念)

    一.插入数据 1.为表的所有字段插入数据 -------------------------------------------------------------------------- (1)i ...

  10. 2019 徐州icpc网络赛 E. XKC's basketball team

    题库链接: https://nanti.jisuanke.com/t/41387 题目大意 给定n个数,与一个数m,求ai右边最后一个至少比ai大m的数与这个数之间有多少个数 思路 对于每一个数,利用 ...