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. 依赖注入框架之androidannotations

    主页: http://androidannotations.org/ 用途: 1. 使用依赖注入Views,extras,System Service,resources 2. 简化线程模型 3. 事 ...

  2. pandas之时间序列(data_range)、重采样(resample)、重组时间序列(PeriodIndex)

    1.data_range生成时间范围 a) pd.date_range(start=None, end=None, periods=None, freq='D') start和end以及freq配合能 ...

  3. SQL学习(三)Select语句:返回前多少行数据

    在实际工作中,我们可能根据某种排序后,只需要显示前多少条数据,此时就需要根据不同的数据库,使用不同的关键字 一.SQL Server/Access select top 数量/百分比 from tab ...

  4. js判断字符串是否为JSON格式

    不能简单地使用来判断字符串是否是JSON格式: function isJSON(str) { if (typeof str == 'string') { try { JSON.parse(str); ...

  5. python中日志logging模块的性能及多进程详解

    python中日志logging模块的性能及多进程详解 使用Python来写后台任务时,时常需要使用输出日志来记录程序运行的状态,并在发生错误时将错误的详细信息保存下来,以别调试和分析.Python的 ...

  6. AES256位加密

    目录 1.    算法简介 2.    算法流程 2.1 扩展密钥 2.2 轮密钥加 2.3 字节代替 2.4 行位移 2.5 列混淆 3.    总结 附录A 运算示例 1.算法简介高级加密标准(英 ...

  7. clientX和clientY属性需要注意的地方

    clientX和clientY为可视区鼠标的位置. 1. 随鼠标移动的div块[runjs] 当document有多个页面时,会出现问题.[runjs] 2. 解决方案:scrollTop, scro ...

  8. 【SVN】导出项目后报错汇总

    原文链接 1.jsp页面内:标点符号,引入报错 解决方法:关闭此项目的jsp验证,右键,最下面一个,Verification,右边一溜只留一个dtd就好 2. 编码问题-乱码 刚拉下来的项目编码可能与 ...

  9. Mac OS下使用pyenv管理Python版本

    问题的由来 在开发过程中,可能会遇到多个版本同时部署的情况. Mac OS自带的Python版本是2.x,自己开发需要Python3.x 系统自带的是2.6.x,开发环境是2.7.x 由于Mac机器系 ...

  10. typeScript入门配置

    typeScript是有微软开发的一款开源的编程语言. TypeScript是JavaScript的一个超集,从今天数以百万计的JavaScript开发者所熟悉的语法和语义开始.可以使用现有的Java ...