LeetCode-Subsets
来源:互联网 发布:智慧树网络课程好过吗? 编辑:程序博客网 时间:2024/06/13 23:01
作者:disappearedgod
文章出处:http://blog.csdn.net/disappearedgod/article/details/39757403
时间:2014-10-3
题目
Subsets
Total Accepted: 23813 Total Submissions: 85835My SubmissionsGiven 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], []]
Subsets
Total Accepted: 23813 Total Submissions: 85835My SubmissionsGiven 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], []]
想法
这里就是简单模拟一个二进制的选择关系。
代码
public class Solution { ArrayList<ArrayList<Integer>> retlist = new ArrayList<ArrayList<Integer>>(); public ArrayList<ArrayList<Integer>> subsets(int[] S) { Arrays.sort(S); int N = 1; for(int i = 0; i < S.length; ++i) N = N << 1; if(S.length == 0) return retlist; for(int i = 0; i < N; i++){// retlist num retlist.add(new ArrayList<Integer>()); for(int res = i,count = 0; res >0 && count < S.length; res = res >> 1,count++) if(res % 2 == 1) retlist.get(i).add(S[count]); } return retlist; }}
结果
My Submissions for Subsets
函数递归
函数递归思路参考了C++代码,但是这里要注意:
Java拷贝时候,list要用clone,否则传递为对象,所以会出现输出重复并不全的现象。
这里体现一个选择与不选择之间的区别。
代码
public class Solution { ArrayList<ArrayList<Integer>> retlist = new ArrayList<ArrayList<Integer>>(); public ArrayList<ArrayList<Integer>> subsets(int[] S) { ArrayList<Integer> list = new ArrayList<Integer>(); Arrays.sort(S); sub(S, 0, new ArrayList<Integer>()); return retlist; } void sub(int[] S, int index, ArrayList<Integer> list){ if(index == S.length){ retlist.add(list); return; } //donot chose S[index] sub(S, index+1, (ArrayList<Integer>)list.clone()); //chose S[index] list.add(S[index]); sub(S, index+1, (ArrayList<Integer>)list.clone()); list.remove(list.size()-1); }}
这里用了一个全集合的概念:
这个概念很像DFS
ArrayList<ArrayList<Integer>> retlist = new ArrayList<ArrayList<Integer>>(); public ArrayList<ArrayList<Integer>> subsets(int[] S) { ArrayList<Integer> list = new ArrayList<Integer>(); Arrays.sort(S); //sub(S, 0, new ArrayList<Integer>()); retlist.add(list); combine( list, S, 0); return retlist; } public void combine( ArrayList<Integer> list, int[] S, int index){ for(int i = index; i < S.length; ++i ){ list.add(S[i]); retlist.add(new ArrayList<Integer>(list));//避免了clone combine(list, S, i+1); list.remove(list.size()-1); } }
class Solution {public: void sub(vector<int> s, int index, vector<int> &path, vector<vector<int>> &solution) {if(index == s.size()){solution.push_back(path);return;}sub(s,index+1,path,solution);path.push_back(s[index]);sub(s,index+1,path,solution);path.pop_back();}vector<vector<int>> subsets(vector<int> &S) { // Note: The Solution object is instantiated only once. vector<vector<int>> solution;vector<int> path; sort(S.begin(),S.end());sub(S,0,path,solution);return solution; }};
结果
My Submissions for Subsets
My Submissions for Subsets
返回
LeetCode Solution(持续更新,java>c++)
一些好的博客
Leetcode Subset I & II
子集系列(一) 传统subset 问题,例 [LeetCode] Subset, Subset II, Bloomberg 的一道面试题
0 0
- 【LeetCode】 Subsets Subsets II
- 【LeetCode】Subsets & Subsets II
- LeetCode Subsets && Subsets II
- LeetCode Subsets && Subsets II
- LeetCode-Subsets & Subsets II
- LeetCode: Subsets
- LeetCode Subsets
- [Leetcode] Subsets
- LeetCode : Subsets
- [LeetCode] Subsets
- Leetcode: Subsets
- Leetcode: Subsets ||
- [leetcode] subsets
- Leetcode - Subsets
- [LeetCode]Subsets
- LeetCode-Subsets
- LeetCode - Subsets
- LeetCode | Subsets
- 黑马程序员--javase高级基础 内省引出的javabean
- Cgi入门教程之:10 email子过程
- UVA 11624.Fire(广搜)
- 遍历Jenkins所有项目的配置
- canada goose parka-714
- LeetCode-Subsets
- Python replace()方法
- UNP卷1:第十四章(高级I/O函数)
- LaTeX完整例子_参考文献、图、表和公式
- 求哈弗曼编码
- hdu 1874 dijsktra(mlogn) +bellmanford
- nginx send back content post by client
- 设置字体相关命令
- <图形图像,动画,多媒体> 读书笔记 --- 音效