【LeetCode】 Subsets Subsets II
2013-10-29 15:43
429 查看
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 =
is:
Discuss
java code :
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 =
is:
Discuss
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); } } }
相关文章推荐
- 十六进制颜色对照表
- Bos启动测试,服务端就绪,客户端不会启动
- java连接mongoDB
- js验证大全(转)
- fstab文件字段小结
- 登陆mysql后,如何查看当前数据库
- 见到C++primer作者Stanley B.Lippman,内心小激动。
- 客户认为OA办公系统是大同小异的几个误解
- 练习:求完数问题
- 科技资讯网站
- C#线程与异步调用
- The instance of keyword
- Android之旅五 ContentProvider和Uri详解
- Android之旅五 ContentProvider和Uri详解
- 项目中图片不显示
- IIs 网站应用程序与虚拟目录的区别及高级应用说明(文件分布式存储方案)
- C++Builder 网站。记住学习
- mongo操作及相关资料
- haffman树
- linux配置java环境变量(详细)