[leedcode 90] Subsets II
2015-07-16 12:33
337 查看
Given a collection of integers that might contain duplicates, nums, 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 nums =
Note:
Elements in a subset must be in non-descending order.
The solution set must not contain duplicate subsets.
For example,
If nums =
[1,2,2], a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], [] ]
public class Solution { List<Integer> seq; List<List<Integer>> res; public List<List<Integer>> subsetsWithDup(int[] nums) { //本题和Subsets的不同就是需要对重复值进行处理,增加了一个判断, //需要注意判断的范围,i首先要满足i>start seq=new ArrayList<Integer>(); res=new ArrayList<List<Integer>>(); Arrays.sort(nums); getSub(nums,0,nums.length); return res; } public void getSub(int[] nums,int start,int end){ if(start<=end){ res.add(new ArrayList<Integer>(seq)); } for(int i=start;i<end;i++){ if(i>start&&nums[i]==nums[i-1])continue; seq.add(nums[i]); getSub(nums,i+1,end); seq.remove(seq.size()-1); //while(i<nums.length-1&&nums[i]==nums[i+1]) i++; } } }
相关文章推荐
- 鬼脚七 两个故事
- 【oc】指针
- 鬼脚七 两个故事
- TraceView分析android应用性能
- 数据结构(一)顺序表、链表以及队列
- 通过独立html用ajax访问基于ssh2中方法的问题
- Linux sh脚本异常:bad interpreter: No such file or directory
- hdu 3496 Watch The Movie
- hdu 3496 Watch The Movie
- 【Oracle 集群】ORACLE DATABASE 11G RAC 知识图文详细教程之集群概念介绍(一)
- 多核程序设计——缓存一致性协议MESI
- mysql更新语句中的safe_mode
- STM32 ST-LINK Utility无法下载的处理方法
- installShield使用
- cpio建立、还原备份档
- Laravel 404错误
- Algorithms—104.Maximum Depth of Binary Tree
- 【Mark】黑客与画家 - 第一章 为什么书呆子不受欢迎
- Android -- Annotation
- Hibernate 学习经验之谈-(四)关联映射