【LeetCode】Subsets I && II
2014-03-01 21:42
351 查看
1、Subsets
Total Accepted: 7967 Total Submissions: 29509 My Submissions
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],
[]
]
2、Subsets II
Total Accepted: 6207 Total Submissions: 23116 My Submissions
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],
[]
]
求子序列集合,一个包含重复数据,一个不包含重复数据。
最后结果要求按照升序排列,并且根据例子来看,应该包含空集。
DFS,没什么可说的。BFS应该也可以解决,不过我没有写代码。有兴趣的可以试试。
这俩题的代码可以写成完全一样的,也是一个好的思维习惯。考虑这种问题的时候,一定要注意重复数据的处理。
这个题和Permutations &&Permutations
II,解题思路可以参考(LeetCode/Permutations && Permutations II)有点类似,本题比这个要更复杂一些。建议先思考简单的,再回来解决本题。
1、Java AC
2、Java AC
Total Accepted: 7967 Total Submissions: 29509 My Submissions
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],
[]
]
2、Subsets II
Total Accepted: 6207 Total Submissions: 23116 My Submissions
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],
[]
]
求子序列集合,一个包含重复数据,一个不包含重复数据。
最后结果要求按照升序排列,并且根据例子来看,应该包含空集。
DFS,没什么可说的。BFS应该也可以解决,不过我没有写代码。有兴趣的可以试试。
这俩题的代码可以写成完全一样的,也是一个好的思维习惯。考虑这种问题的时候,一定要注意重复数据的处理。
这个题和Permutations &&Permutations
II,解题思路可以参考(LeetCode/Permutations && Permutations II)有点类似,本题比这个要更复杂一些。建议先思考简单的,再回来解决本题。
1、Java AC
public class Solution { public int len; public ArrayList<ArrayList<Integer>> subsets(int[] S) { ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); if (S == null || S.length == 0) { return list; } len = S.length; Arrays.sort(S); for (int i = 1; i < len + 1; i++) { ArrayList<Integer> numList = new ArrayList<Integer>(); dfs(list, numList, S, 0, i); } list.add(new ArrayList<Integer>()); return list; } public void dfs(ArrayList<ArrayList<Integer>> list, ArrayList<Integer> numList, int[] S, int start, int k) { if (numList.size() == k) { list.add(new ArrayList<Integer>(numList)); return; } ArrayList<Integer> allList = new ArrayList<Integer>(); for (int i = start; i < len; i++) { if (allList.contains(S[i])) { continue; } allList.add(S[i]); numList.add(S[i]); dfs(list, numList, S, i + 1, k); numList.remove(numList.size() - 1); } } }
2、Java AC
public class Solution { public int len; public ArrayList<ArrayList<Integer>> subsetsWithDup(int[] num) { ArrayList<ArrayList<Integer>> list = new ArrayList<ArrayList<Integer>>(); if (num == null || num.length == 0) { return list; } len = num.length; Arrays.sort(num); for (int i = 1; i < len + 1; i++) { ArrayList<Integer> numList = new ArrayList<Integer>(); dfs(list, numList, num, 0, i); } list.add(new ArrayList<Integer>()); return list; } public void dfs(ArrayList<ArrayList<Integer>> list, ArrayList<Integer> numList, int[] num, int start, int k) { if (numList.size() == k) { list.add(new ArrayList<Integer>(numList)); return; } ArrayList<Integer> allList = new ArrayList<Integer>(); for (int i = start; i < len; i++) { if (allList.contains(num[i])) { continue; } allList.add(num[i]); numList.add(num[i]); dfs(list, numList, num, i + 1, k); numList.remove(numList.size() - 1); } } }
相关文章推荐
- leetcode -- Subsets I &II-- 重点,求0,1序列
- Leetcode | Subsets I & II
- LeetCode: Subsets I & II
- 【leetcode】Subsets I & II
- [leetcode 78 & 90, Medium] Subsets I and II
- [LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法
- LeetCode - Subsets I && II
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
- Leetcode 78&90. Subsets I & II 【排列与组合的生成总结】
- [Leetcode]Subsets I&II
- [Leetcode] #78#90 Subsets I & II
- <LeetCode OJ> 78 / 90 Subsets (I / II)
- LeetCode: Linked List Cycle I && II
- Leetcode: Unique Path I & II, Minimum Path Sum, Triangle
- [LeetCode]Search in Rotated Sorted Array I && II
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- 【Leetcode】92. Reverse Linked List II && 206. Reverse Linked List
- leetcode之 Palindrome Partitioning I&II
- LeetCode Combination Sum & Combination Sum II
- [LeetCode]-Combination Sum I&II 求相加和为target的集合