-
Notifications
You must be signed in to change notification settings - Fork 9
/
CombinationsWithDuplicates.cpp
84 lines (75 loc) · 2.53 KB
/
CombinationsWithDuplicates.cpp
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
// Problem: https://leetcode.com/problems/permutations-ii/
#include <string>
#include <vector>
using namespace std;
class CombinationsWithDuplicates {
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
vector<vector<int>> result;
permuteNext(nums, result, 0 /* start */);
getUnique(result);
return result;
}
private:
void getUnique(vector<vector<int>>& result) {
unordered_set<string> result_str;
for (int i = 0; i < result.size(); ++i) result_str.insert(toString(result[i]));
result.clear();
unordered_set<string>::iterator it;
for (it = result_str.begin(); it != result_str.end(); ++it) {
result.push_back(toVector(*it));
}
}
string toString(const vector<int>& vec_str) {
string str;
for (int i = 0; i < vec_str.size(); ++i) str += to_string(vec_str[i]) + ",";
return str;
}
vector<int> toVector(const string& str) {
string tmp;
vector<int> result;
stringstream ss(str);
while (getline(ss, tmp, ',')) result.push_back(toInteger(tmp));
return result;
}
int toInteger(const string& str) {
int result = 0;
if (str.empty()) return result;
int index = 0;
bool is_negative = (str[0] == '-');
while (index < str.size()) {
if (isdigit(str[index])) {
int digit = (int)(str[index] - '0');
result = result * 10 + digit;
}
++index;
}
if (is_negative) return (-1 * result);
return result;
}
void permuteNext(const vector<int>& nums, vector<vector<int>>& result, int s) {
// Loop termination conditions.
if (s == nums.size()) return;
if (s == (nums.size() - 1)) {
result.push_back({nums.back()});
return;
}
permuteNext(nums, result, s + 1);
vector<vector<int>> new_result;
for (int i = 0; i < result.size(); ++i) {
vector<vector<int>> u_result = getUpdated(nums[s], result[i]);
new_result.insert(new_result.end(), u_result.begin(), u_result.end());
}
result.clear();
result = new_result;
}
vector<vector<int>> getUpdated(int num, const vector<int>& result) {
vector<vector<int>> u_result;
for (int i = 0; i <= result.size(); ++i) {
vector<int> tmp = result;
tmp.insert(tmp.begin() + i, num);
u_result.push_back(tmp);
}
return u_result;
}
};