-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathpermutations_ii.cpp
56 lines (50 loc) · 1.49 KB
/
permutations_ii.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
class Solution {
public:
// 原理分析
// 标准的排列组合就不说了,典型的递归求解。
// 现在要求不重复,以[1, 1, 2, 3]为例
// [1], [1, 2, 3]
// - [1, 1], [2, 3]
// - [1, 1, 2], [3] => [1, 1, 2, 3]
// - [1, 1, 3], [2] => [1, 1, 3, 2]
// 重新排序,还原成[1, 1, 2, 3]
// [2], [1, 1, 3]
// - [2, 1], [1, 3]
// - [2, 1, 1], [3] => [2, 1, 1, 3]
// - [2, 1, 3], [1] => [2, 1, 3, 1]
// 重新排序,还原成[1, 1, 2, 3]
// [3], [1, 1, 2]
// - [3, 1], [1, 2]
// - [3, 1, 1], [2] => [3, 1, 1, 2]
// - [3, 1, 2], [1] => [3, 1, 2, 1]
void generate_permutation_unique(vector<int> &num,
int index,
vector<int> &permutation,
vector<vector<int> > &results) {
int size = num.size();
if (size == index) {
results.push_back(permutation);
}
else {
for (int i = index; i < size; ++i) {
if ((i > index) && (num[i] == num[index])) {
continue;
}
else {
swap(num[index], num[i]);
}
permutation.push_back(num[index]);
generate_permutation_unique(num, index + 1, permutation, results);
permutation.pop_back();
}
sort(num.begin() + index, num.end());
}
}
vector<vector<int> > permuteUnique(vector<int> &num) {
vector<vector<int> > results;
vector<int> permutation;
sort(num.begin(), num.end());
generate_permutation_unique(num, 0, permutation, results);
return results;
}
};