-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubsets_II.cpp
More file actions
87 lines (76 loc) · 1.87 KB
/
Subsets_II.cpp
File metadata and controls
87 lines (76 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Source : https://oj.leetcode.com/problems/subsets-ii/
// Author : zheng yi xiong
// Date : 2014-12-31
/**********************************************************************************
*
* 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 = [1,2,2], a solution is:
* [
* [2],
* [1],
* [1,2,2],
* [2,2],
* [1,2],
* []
* ]
*
**********************************************************************************/
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
vector<vector<int> > subsetsWithDup(vector<int> &S) {
sort(S.begin(), S.end());
vector<vector<int> > vec_vec_int;
vector<int> temp_vec;
vec_vec_int.push_back(temp_vec);
int index = 0;
SubVecDup(vec_vec_int, S, index);
return vec_vec_int;
}
private:
void SubVecDup(vector<vector<int> > &vec_vec_int, vector<int> &S, int &index)
{
int vec_vec_len = vec_vec_int.size();
int vec_vec_index = 0;
for (int i = index; i < S.size() - 1; ++i)
{
for (int j = 0; j < vec_vec_len; ++j)
{
vector<int> temp_vec(vec_vec_int[vec_vec_index + j]);
temp_vec.push_back(S[i]);
vec_vec_int.push_back(temp_vec);
}
if (S[i + 1] != S[i])
{
index = i + 1;
SubVecDup(vec_vec_int, S, index);
return;
}
vec_vec_index += vec_vec_len;
}
for (int j = 0; j < vec_vec_len; ++j)
{
vector<int> temp_vec(vec_vec_int[vec_vec_index + j]);
temp_vec.push_back(S.back());
vec_vec_int.push_back(temp_vec);
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> S;
S.push_back(1);
S.push_back(2);
S.push_back(2);
Solution so;
vector<vector<int> > vec_vec_int = so.subsetsWithDup(S);
return 0;
}