-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpermutations-ii.cpp
40 lines (32 loc) · 1.1 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
class Solution {
// https://stackoverflow.com/questions/11483060/stdnext-permutation-implementation-explanation
// https://www.techiedelight.com/std_next_permutation-overview-implementation
// 1 2 3 4
// 1 2 4 3
// 1 3 2 4
// 1 3 4 2
// 1 4 2 3
// 1 4 3 2
// 2 1 3 4
bool nextPermutation(vector<int>& nums) {
int lastIndex = size(nums) - 1;
if (!lastIndex) return false;
while(nums[lastIndex-1] >= nums[lastIndex])
if(--lastIndex == 0) return false;
int toSwapWith = size(nums) - 1;
while(toSwapWith >lastIndex
&& nums[lastIndex - 1] >= nums[toSwapWith]) toSwapWith--;
swap(nums[lastIndex - 1], nums[toSwapWith]);
reverse(nums.begin() + lastIndex, nums.end());
return true;
}
public:
vector<vector<int>> permuteUnique(vector<int>& nums) {
sort(begin(nums), end(nums));
vector<vector<int>> result;
do {
result.push_back(nums);
} while (nextPermutation(nums));
return result;
}
};