-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
51 lines (50 loc) · 1.29 KB
/
solution.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
class Solution
{
public:
vector<int> nextPermutation(vector<int> &num)
{
if(num.size() < 2)
return num;
int curMax = num[num.size() - 1];
for(int i = num.size() - 1; i >= 0;i--)
{
if(num[i] < curMax)
{
int tmp = num[i];
int j = 0;
for(j = i + 1; j < num.size();j++)
{
if(tmp < num[j])
{
num[i] = num[j];
num[j] = tmp;
return num;
}
}
if(j == num.size())
{
num[j - 1] = tmp;
return num;
}
}
else
{
curMax = num[i];
for(int j = i + 1; j < num.size();j++)
num[j - 1] = num[j];
num[num.size() - 1] = curMax;
}
}
return num;
}
vector<vector<int> > permute(vector<int> &num)
{
long long n = num.size();
for(int i=n-1;i>0;i--)
n *= i;
vector<vector<int> > result;
while(n--)
result.push_back(nextPermutation(num));
return result;
}
};