LeetCode--Subsets II
2015-01-13 17:00
363 查看
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 =
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], [] ]
class Solution { public: vector<vector<int> > subsetsWithDup(vector<int> &S) { int n = S.size(); heap_sort(S); vector<vector<int>> res; for(int k=0; k<=n; k++) { vector<int> temp(k,-1); get_res(-1,0,k,S,res,temp); } return res; } void get_res(int loc, int now, int k, vector<int>& s, vector<vector<int>>& res, vector<int>& temp) { if(k == now) { res.push_back(temp); return; } int n = s.size(); temp[now] = s[loc+1]; get_res(loc+1,now+1,k,s,res,temp); for(int i=loc+2; i<=n-k+now; i++) { if(s[i] == s[i-1]) continue; temp[now] = s[i]; get_res(i,now+1,k,s,res,temp); } return; } void swap(vector<int>& s, int i, int j) { int t = s[i]; s[i] = s[j]; s[j] = t; } void sink(vector<int>& s, int loc, int end) { while(loc<end) { int k=2*loc+1; if(k>end) return ; if(k<end && s[k+1]>s[k]) k++; if(s[loc] > s[k]) return ; swap(s,k,loc); loc = k; } } void heap_sort(vector<int>& s) { int n = s.size()-1; for(int i=(n-1)/2; i>=0; i--) sink(s,i,n); while(n>0) { swap(s,0,n); n--; sink(s,0,n); } } };
相关文章推荐
- Leetcode 90 Subsets II (打印不重复的子集)
- leetcode_90题——Subsets II (递推)
- LeetCode-Subsets II
- LeetCode-90. Subsets II
- [leetcode]Subsets II
- Leetcode : Subsets II
- LeetCode: Subsets II [091]
- Subsets II -- LeetCode
- LeetCode----Subsets II
- leetcode90:Subsets II
- LeetCode: Subsets II
- leetcode:Subsets II
- leetcode第一刷_Subsets II
- 80 leetcode - Subsets II
- 【leetcode】【90】Subsets II
- Subsets II (leetcode)
- LeetCode (Subsets II)
- ***LeetCode 90 Subsets ii
- LeetCode *** 90. Subsets II
- 【LeetCode】Subsets II 解题报告