-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathPermutations II.cpp
More file actions
40 lines (36 loc) · 965 Bytes
/
Permutations II.cpp
File metadata and controls
40 lines (36 loc) · 965 Bytes
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
{
public:
vector<vector<int> > permuteUnique(vector<int> &num)
{
vector<int> one(num);
sort(one.begin(), one.end());
vector<vector<int>> all({one});
const size_t count = one.size();
while (true)
{
bool find = false;
for (size_t i = count - 1; i > 0; --i)
{
if (one[i] > one[i-1])
{
size_t j = count - 1;
while (one[j] <= one[i - 1])
{
--j;
}
swap(one[i - 1], one[j]);
reverse(one.begin() + i, one.end());
all.push_back(one);
find = true;
break;
}
}
if (!find)
{
break;
}
}
return all;
}
};