-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLeetCode#28.cc
More file actions
28 lines (28 loc) · 819 Bytes
/
Copy pathLeetCode#28.cc
File metadata and controls
28 lines (28 loc) · 819 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
class Solution {
public:
void nextPermutation(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int size = num.size();
if(size<=1) return ;
int _max = num[size-1];
for(int i=size-2;i>=0;i--){
if(num[i]>=_max){
_max = num[i];
}
else{
int ind = -1;
for(int j=i+1;j<size;j++)
if(num[j]>num[i]&&(ind==-1 || num[ind]>num[j]))
ind = j;
int tmp = num[ind];
num[ind] = num[i];
num[i] = tmp;
sort(num.begin()+i+1,num.end());
return ;
}
}
sort(num.begin(),num.end());
return ;
}
};