78. Subsets
题目:
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], [] ]
链接: http://leetcode.com/problems/subsets/
题解:
求数组子数组。先把数组排序,之后就可以使用DFS,维护一个递增的position,递归后要backtracking。
Time Complexity - O(n * 2n), Space Complexity - O(n)
public class Solution { public ArrayList<ArrayList<Integer>> subsets(int[] S) { ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>(); if(S == null || S.length == 0) return result; ArrayList<Integer> list = new ArrayList<Integer>(); Arrays.sort(S); helper(result, list, S, 0); return result; } private void helper(ArrayList<ArrayList<Integer>> result, ArrayList<Integer> list, int[] S, int pos){ result.add(new ArrayList<Integer>(list)); for(int i = pos; i < S.length; i ++){ list.add(S[i]); helper(result, list, S, ++pos); list.remove(list.size() - 1); } } }
Updates:
public class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res = new ArrayList<>(); if(nums == null || nums.length == 0) return res; Arrays.sort(nums); ArrayList<Integer> list = new ArrayList<>(); dfs(res, list, nums, 0); return res; } private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] nums, int pos) { res.add(new ArrayList<Integer>(list)); for(int i = pos; i < nums.length; i++) { list.add(nums[i]); dfs(res, list, nums, ++pos); list.remove(list.size() - 1); } } }
Update:
为什么以前总写成++pos? i + 1就可以了
public class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res = new ArrayList<>(); if(nums == null || nums.length == 0) return res; Arrays.sort(nums); ArrayList<Integer> list = new ArrayList<>(); dfs(res, list, nums, 0); return res; } private void dfs(List<List<Integer>> res, ArrayList<Integer> list, int[] nums, int pos) { res.add(new ArrayList<Integer>(list)); for(int i = pos; i < nums.length; i++) { list.add(nums[i]); dfs(res, list, nums, i + 1); list.remove(list.size() - 1); } } }
二刷:
发现自己以前不懂装懂糊弄过去了好多题...我勒个去。
这道题目我们也是使用跟上一题combination类似的方法。
- 这里我们根据题意首先要对数组排个序
- 构造一个辅助函数getSubsets来进行DFS和backtracking, 同时这个辅助函数还要有一个pos来控制遍历的位置,我们先pass 0 进去。
- 每次进入getSubsets我们都直接往结果集中加入一个当前List的新副本
- 接下来从pos开始遍历整个数组,每次进入新一层dfs的时候pass 新的pos = i + 1,这样就能保证结果中的顺序是从小到大
Java:
Time Complexity - O(n!), Space Complexity (n2)
public class Solution { public List<List<Integer>> subsets(int[] nums) { List<List<Integer>> res = new ArrayList<>(); if (nums == null || nums.length == 0) { return res; } Arrays.sort(nums); List<Integer> list = new ArrayList<>(); getSubsets(res, list, nums, 0); return res; } private void getSubsets(List<List<Integer>> res, List<Integer> list, int[] nums, int pos) { res.add(new ArrayList<Integer>(list)); for (int i = pos; i < nums.length; i++) { list.add(nums[i]); getSubsets(res, list, nums, i + 1); list.remove(list.size() - 1); } } }
三刷:
下次还需要研究Bit Manipulation 以及 iterative的写法。
Java:
Time Complexity - O(n * 2n), Space Complexity (2n)
class Solution {
public List<List<Integer>> subsets(int[] nums) {
List<List<Integer>> res = new ArrayList<>();
if (nums == null || nums.length == 0) return res;
subsets(res, nums, new ArrayList<Integer>(), 0);
return res;
}
private void subsets(List<List<Integer>> res, int[] nums, List<Integer> list, int idx) {
res.add(new ArrayList<>(list));
for (int i = idx; i < nums.length; i++) {
list.add(nums[i]);
subsets(res, nums, list, i + 1);
list.remove(list.size() - 1);
}
}
}
测试:
Reference:
https://leetcode.com/discuss/72498/simple-iteration-no-recursion-no-twiddling-explanation
https://leetcode.com/discuss/25696/simple-java-solution-with-for-each-loops
https://leetcode.com/discuss/29631/java-subsets-solution
https://leetcode.com/discuss/46668/recursive-iterative-manipulation-solutions-explanations
https://leetcode.com/discuss/9213/my-solution-using-bit-manipulation
http://www.cnblogs.com/springfor/p/3879830.html
http://www.cnblogs.com/zhuli19901106/p/3492515.html
http://www.1point3acres.com/bbs/thread-117602-1-1.html

浙公网安备 33010602011771号