LeetCode - Subsets I && II
2015-03-13 01:36
465 查看
https://leetcode.com/problems/subsets/
这道题比较典型,一般需要all, every之类所有结果的,都可以用递归做,因为题目要求结果要升序,所以在做之前对数组排序,并且在做之前把空集放进去。
注意递归调用helper那行,下一个start不应该是start+1,而是i+1
附一个不用递归的方法:
Given a set S of n distinct integers, there is a relation between Sn and Sn-1. The subset of Sn-1 is the union of {subset of Sn-1} and {each element in Sn-1 + one more element}. Therefore,
a Java solution can be quickly formalized.
http://www.programcreek.com/2013/01/leetcode-subsets-java/
一亩三分地上对这道题时间复杂度分析:
http://www.1point3acres.com/bbs/thread-117602-1-1.html
时间复杂度分析看得不是很懂,todo:把在做完combination之后把这篇分析看懂!
Subsets II
https://leetcode.com/problems/subsets-ii/
跟Subsets I 基本完全一样,只需要在循环到下一个数时,略掉重复数就可以避免重复子集,注意,进入下一层递归时,是不需要略掉重复数的,因为一个子集里可以有重复元素。
这道题比较典型,一般需要all, every之类所有结果的,都可以用递归做,因为题目要求结果要升序,所以在做之前对数组排序,并且在做之前把空集放进去。
注意递归调用helper那行,下一个start不应该是start+1,而是i+1
public List<List<Integer>> subsets(int[] S) { List<List<Integer>> rst = new LinkedList<List<Integer>>(); if(S==null || S.length==0) return rst; List<Integer> sol = new LinkedList<Integer>(); Arrays.sort(S); // to ensure ascending order in the result list rst.add(sol); helper(S, 0, rst, sol); return rst; } public void helper(int[] S, int start, List<List<Integer>> rst, List<Integer> sol){ for(int i=start; i<S.length; i++){ sol.add(S[i]); rst.add(new LinkedList<Integer>(sol)); helper(S, i+1, rst, sol); //!!!should be i+1, instead of start+1 here sol.remove(sol.size()-1); } }
附一个不用递归的方法:
Given a set S of n distinct integers, there is a relation between Sn and Sn-1. The subset of Sn-1 is the union of {subset of Sn-1} and {each element in Sn-1 + one more element}. Therefore,
a Java solution can be quickly formalized.
http://www.programcreek.com/2013/01/leetcode-subsets-java/
一亩三分地上对这道题时间复杂度分析:
http://www.1point3acres.com/bbs/thread-117602-1-1.html
时间复杂度分析看得不是很懂,todo:把在做完combination之后把这篇分析看懂!
Subsets II
https://leetcode.com/problems/subsets-ii/
跟Subsets I 基本完全一样,只需要在循环到下一个数时,略掉重复数就可以避免重复子集,注意,进入下一层递归时,是不需要略掉重复数的,因为一个子集里可以有重复元素。
public List<List<Integer>> subsetsWithDup(int[] S) { List<List<Integer>> rst = new LinkedList<List<Integer>>(); if(S==null || S.length==0) return rst; List<Integer> sol = new LinkedList<Integer>(); Arrays.sort(S); // to ensure ascending order in the result list rst.add(sol); helper(S, 0, rst, sol); return rst; } public void helper(int[] S, int start, List<List<Integer>> rst, List<Integer> sol){ for(int i=start; i<S.length; i++){ sol.add(S[i]); rst.add(new LinkedList<Integer>(sol)); helper(S, i+1, rst, sol); //!!!should be i+1, instead of start+1 here sol.remove(sol.size()-1); while((i+1)<S.length && S[i+1]==S[i]) i++; } }
相关文章推荐
- LeetCode: Subsets I & II
- <LeetCode OJ> 78 / 90 Subsets (I / II)
- 【leetcode】Subsets I & II
- [leetcode 78 & 90, Medium] Subsets I and II
- [LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法
- LeetCode解题报告—— Word Search & Subsets II & Decode Ways
- Leetcode | Subsets I & II
- [Leetcode] #78#90 Subsets I & II
- leetcode -- Subsets I &II-- 重点,求0,1序列
- [Leetcode]Subsets I&II
- Leetcode 78&90. Subsets I & II 【排列与组合的生成总结】
- 【LeetCode】Subsets I && II
- [算法][LeetCode]Linked List Cycle & Linked List Cycle II——单链表中的环
- LeetCode题解:Subsets I and II
- [LeetCode 112 113] - 路径和I & II (Path Sum I & II)
- LeetCode Single Number I & II 都符合两个问题额外要求的 通用解法 与 思考过程
- 【LeetCode】 Subsets Subsets II
- Leetcode: N Queen I & II
- leetcode Best Time to Buy and Sell Stock I&&II&&III
- [LeetCode 116 117] - 填充每一个节点的指向右边邻居的指针I & II (Populating Next Right Pointers in Each Node I & II)