LeetCode Online Judge 题目C# 练习 - Subsets
2012-10-18 22:38
369 查看
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],
[]
]
代码分析:
DP,第一次做居然想不到,看来复习很重要。 用一个prev_count记录上一次DP的时候做到哪里,不然ret.Count 在里往里 Add 的时候会不停增加的。
代码分析:
这就是之前想的递归方法,没多大区别。而且还不符合题目 non-descending 的要求。因为递归是从最后一个element开始加的。如果要用这个方法,就要把输入数组 S,descendly sort一次,这里要用到delegate 做 comparsion. 我就懒得做了。
代码分析:
介绍一种比较不一样的方法——Combinatoric。
例子: S = { 1, 2, 3}
i = 0 : (1 << S.Count - 1) = 0 : 111 (就是S有3个数,i 最大到 111, 4个数, i 最大就到 1111 。。。)
然后 k = i; k & 1 == 1 就把相应 S[index] 加到答案里面。然后k >>= 1;
k = 0;(0x0) {}
k = 1;(0x1) {1}
k = 2;(0x10) {2},
k = 3;(0x11) {1,2};
k = 4;(0x100) {3};
k = 5;(0x101) {1,3};
k = 6;(0x110) {2,3};
k = 7;(0x111) {1,2,3};
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 static List<List<int>> Subsets2(List<int> S) { S.Sort(); List<List<int>> ret = new List<List<int>>(); List<int> empty = new List<int>(); ret.Add(empty); for (int i = 0; i < S.Count; i++) { int prev_count = ret.Count; for (int j = 0; j < prev_count; j++) { List<int> temp = new List<int>(ret[j]); temp.Add(S[i]); ret.Add(temp); } } return ret; }
代码分析:
DP,第一次做居然想不到,看来复习很重要。 用一个prev_count记录上一次DP的时候做到哪里,不然ret.Count 在里往里 Add 的时候会不停增加的。
public static List<List<int>> Subsets(List<int> S) { List<List<int>> ret = new List<List<int>>(); SubsetsRecursive(ret, S, 0); return ret; } public static void SubsetsRecursive(List<List<int>> ret, List<int> S, int index) { if (index == S.Count) ret.Add(new List<int>()); else { SubsetsRecursive(ret, S, index + 1); int n = ret.Count; for (int i = 0; i < n; i++) { List<int> temp = new List<int>(ret[i]); temp.Add(S[index]); ret.Add(temp); } } }
代码分析:
这就是之前想的递归方法,没多大区别。而且还不符合题目 non-descending 的要求。因为递归是从最后一个element开始加的。如果要用这个方法,就要把输入数组 S,descendly sort一次,这里要用到delegate 做 comparsion. 我就懒得做了。
public static List<List<int>> SubsetsCombinatoric(List<int> S) { S.Sort(); List<List<int>> ret = new List<List<int>>(); for (int i = 0; i < (1 << S.Count); i++) { int k = i; int index = 0; List<int> subset = new List<int>(); while (k > 0) { if ((k & 1) > 0) { subset.Add(S[index]); } k >>= 1; index++; } ret.Add(subset); } return ret; }
代码分析:
介绍一种比较不一样的方法——Combinatoric。
例子: S = { 1, 2, 3}
i = 0 : (1 << S.Count - 1) = 0 : 111 (就是S有3个数,i 最大到 111, 4个数, i 最大就到 1111 。。。)
然后 k = i; k & 1 == 1 就把相应 S[index] 加到答案里面。然后k >>= 1;
k = 0;(0x0) {}
k = 1;(0x1) {1}
k = 2;(0x10) {2},
k = 3;(0x11) {1,2};
k = 4;(0x100) {3};
k = 5;(0x101) {1,3};
k = 6;(0x110) {2,3};
k = 7;(0x111) {1,2,3};
相关文章推荐
- LeetCode Online Judge 题目C# 练习 - Minimum Window Substring
- LeetCode Online Judge 题目C# 练习 - Remove Element
- LeetCode Online Judge 题目C# 练习 - Count and Say
- LeetCode Online Judge 题目C# 练习 - Decode Way
- LeetCode Online Judge 题目C# 练习 - Plus One
- LeetCode Online Judge 题目C# 练习 - Search Insert Position
- LeetCode Online Judge 题目C# 练习 - Unique Binary Search Trees II
- LeetCode Online Judge 题目C# 练习 - Climbing Stairs
- LeetCode Online Judge 题目C# 练习 - Median of Two Sorted Arrays
- LeetCode Online Judge 题目C# 练习 - Merge k Sorted Lists
- LeetCode Online Judge 题目C# 练习 - Pow(x, n)
- LeetCode Online Judge 题目C# 练习 - Set Matrix Zeroes
- LeetCode Online Judge 题目C# 练习 - Unique Paths
- LeetCode Online Judge 题目C# 练习 - Divid Two Integer
- LeetCode Online Judge 题目C# 练习 - Anagrams
- LeetCode Online Judge 题目C# 练习 - Maximal Rectangle
- LeetCode Online Judge 题目C# 练习 - Maximum Subarray
- LeetCode Online Judge 题目C# 练习 - Merge Sorted Array
- LeetCode Online Judge 题目C# 练习 - Valid Number
- LeetCode Online Judge 题目C# 练习 - Same Tree