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;
}

最新文章

  1. Nginx内置变量
  2. JS 之Blob 对象类型
  3. Android -- 通知栏的使用
  4. UIImageView、UISlider、UISwitch、UIStepper、UISegmentControl
  5. 【CodeForces 622A】Infinite Sequence
  6. 每日一九度之 题目1040:Prime Number
  7. 使用SurfaceView
  8. ndk android studio万年坑
  9. Citrix 服务器虚拟化之五 Xenserver配置存储
  10. 李洪强iOS开发之OC[010] - 有参方法的声明实现和调用
  11. 【转】创业C2C(Copy To China):停车位共享APP,用户、市政能够买账?
  12. LINUX下中文语言包的安装(转)
  13. 开源轻量级移动端友好的JS地图库——leaflet学习教程
  14. cocos2d-x学习过程中的疑问
  15. Java 泛型 泛型的约束与局限性
  16. Struts2透过自定义拦截器实现登录之后跳转到原页面
  17. pickle模块的使用python3
  18. [置顶] 一个demo学会c#
  19. bzoj 3924: [Zjoi2015]幻想乡战略游戏
  20. bzoj 4767 两双手 - 动态规划 - 容斥原理

热门文章

  1. GitHub:Baidu
  2. 解决 JDK1.7 不支持 VCenter 6.7 的问题(涉及到Https TLS1.2协议)
  3. SAS数据挖掘实战篇【一】
  4. &quot;首页添加至购物车,TabBar显示购物车的数量&quot;实现
  5. php register_shutdown_function响应error 配合error_get_last 捕获错误
  6. Goland 开发插件安装
  7. kibana 查询例子
  8. alembic常用命令和经典错误解决办法
  9. linux 下文件上传的两种工具(XFTP5和Putty之pscp)方式
  10. Quartz持久化到mongodb