-
Notifications
You must be signed in to change notification settings - Fork 77
/
solution.cpp
38 lines (38 loc) · 987 Bytes
/
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
class Solution
{
public:
void nextPermutation(vector<int> &num)
{
if(num.size() < 2) return;
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;
}
}
if(j == num.size())
{
num[j - 1] = tmp;
return;
}
}
else
{
curMax = num[i];
for(int j = i + 1; j < num.size();j++)
num[j - 1] = num[j];
num[num.size() - 1] = curMax;
}
}
}
};