【LeetCode】 Subsets Subsets II
来源:互联网 发布:solarman软件下载 编辑:程序博客网 时间:2024/05/01 11:25
Given a set of distinct integers, S, 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 S = [1,2,3]
, a solution is:
[ [3], [1], [2], [1,2,3], [1,3], [2,3], [1,2], []]
Discuss
java code :
public class Solution { public ArrayList<ArrayList<Integer>> subsets(int[] S) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();res.add(new ArrayList<Integer>());if(S.length ==0)return res;Arrays.sort(S);ArrayList<Integer> tmp = new ArrayList<Integer>();for(int i = 1; i <= S.length; i++){tmp.clear();recursion(res,tmp,i,S,0);}return res; } public void recursion(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> tmp, int k, int[] S, int dp){if(k == tmp.size()){res.add(new ArrayList<Integer>(tmp));return ;}for(int i = dp; i < S.length; i++){tmp.add(S[i]);recursion(res,tmp,k,S,i+1);tmp.remove(tmp.size() - 1);}}}
Given a collection of integers that might contain duplicates, S, 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 S = [1,2,2]
, a solution is:
[ [2], [1], [1,2,2], [2,2], [1,2], []]
Discuss
public class Solution { public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) { // IMPORTANT: Please reset any member data you declared, as // the same Solution instance will be reused for each test case. ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();res.add(new ArrayList<Integer>());if(num.length ==0)return res;Arrays.sort(num);ArrayList<Integer> tmp = new ArrayList<Integer>();for(int i = 1; i <= num.length; i++){tmp.clear();recursion(res,tmp,i,num,0);}return res; } public void recursion(ArrayList<ArrayList<Integer>> res, ArrayList<Integer> tmp, int k, int[] S, int dp){if(k == tmp.size()){if(!res.contains(tmp))res.add(new ArrayList<Integer>(tmp));return ;}for(int i = dp; i < S.length; i++){tmp.add(S[i]);recursion(res,tmp,k,S,i+1);tmp.remove(tmp.size() - 1);}}}
- 【LeetCode】 Subsets Subsets II
- 【LeetCode】Subsets & Subsets II
- LeetCode Subsets && Subsets II
- LeetCode Subsets && Subsets II
- LeetCode-Subsets & Subsets II
- [LeetCode] Combinations、Subsets、Subsets II
- 【Leetcode】【python】Subsets/Subsets II
- LeetCode Subsets 和 LeetCode Subsets II
- [LeetCode 78] Subsets && [LeetCode 90] Subsets II
- LeetCode: Subsets II
- LeetCode SubSets II
- LeetCode: Subsets II
- leetcode 48: Subsets II
- [LeetCode] Subsets II
- [Leetcode] Subsets II
- [leetcode] Subsets II
- leetcode Subsets II
- leetcode Subsets II problem
- haffman树
- The instance of keyword
- 十六进制颜色对照表
- js验证大全(转)
- Cocos2D_X中的文本操作(LibJson)
- 【LeetCode】 Subsets Subsets II
- Oracle常用SQL(DDL)命令
- Java中二维数组的长度
- ubuntu 13.10 & Ubuntu 14.04 64位安装及配置 google拼音输入法及Adobe flash player、mp3 插件安装、中文GBK编码等
- EASYUI+MVC4+VS2010通用权限管理系统开发
- Bos启动测试,服务端就绪,客户端不会启动
- PorterDuff.Mode
- POJ 3728 The merchant
- hibernate中session的get()、load()方法比较