LeetCode: Subsets
2013-04-21 11:26
288 查看
脑子有点混,少数次过
C#
View Code
class Solution { public: void dfs(int cur, int n, vector<vector<int>> &ret, vector<int> &tmp, vector<int> S) { if (cur == n) { ret.push_back(tmp); return; } dfs(cur+1, n, ret, tmp, S); tmp.push_back(S[cur]); dfs(cur+1, n, ret, tmp, S); tmp.pop_back(); } vector<vector<int> > subsets(vector<int> &S) { // Start typing your C/C++ solution below // DO NOT write int main() function sort(S.begin(), S.end()); vector<vector<int>> ret; if (!S.size()) return ret; vector<int> tmp; dfs(0, S.size(), ret, tmp, S); return ret; } };
C#
public class Solution { public List<List<int>> Subsets(int[] nums) { Array.Sort(nums); List<List<int>> ans = new List<List<int>>(); if (nums.Length == 0) return ans; List<int> tmp = new List<int>(); dfs(0, nums.Length, ref ans, ref tmp, nums); return ans; } public void dfs(int cur, int n, ref List<List<int>> ans, ref List<int> tmp, int[] nums) { if (cur == n) { ans.Add(new List<int>(tmp.ToArray())); return; } dfs(cur+1, n, ref ans, ref tmp, nums); tmp.Add(nums[cur]); dfs(cur+1, n, ref ans, ref tmp, nums); tmp.RemoveAt(tmp.Count - 1); } }
View Code
相关文章推荐
- [LeetCode] Partition to K Equal Sum Subsets 分割K个等和的子集
- 【VIP】【leetcode题解】【回溯】【97.5】【M】Subsets
- leetcode-78-subsets
- [LeetCode] Subsets I (78) & II (90) 解题思路,即全组合算法
- LeetCode - Subsets
- leetcode--Subsets
- LeetCode Online Judge 题目C# 练习 - Subsets
- LeetCode(78) Subsets
- 【leetcode】Subsets
- LeetCode OJ Subsets
- LeetCode--Subsets
- leetcode78、90-Subsets I/II(组合数/子集数目)
- LeetCode(078) Subsets (Java)
- Subsets [LeetCode]
- LeetCode:Subsets
- [LeetCode]Subsets
- leetcode:Subsets
- leetcode-78-Subsets
- [leetcode] 78.Subsets
- 078 Subsets [Leetcode]