Skip to content

Commit 0172de0

Browse files
committed
Time: 0 ms (100.00%), Space: 12 MB (93.30%) - LeetHub
1 parent b97d907 commit 0172de0

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Solution {
2+
public:
3+
void nextPermutation(vector<int>& nums) {
4+
//
5+
int idx = -1;
6+
int n = nums.size();
7+
for(int i = n - 1; i > 0; i--){
8+
if(nums[i - 1] < nums[i]){
9+
idx = i - 1;
10+
break;
11+
}
12+
}
13+
14+
if(idx == -1){
15+
16+
reverse(nums.begin(), nums.end());
17+
return;
18+
}
19+
20+
for(int i = n - 1; i > idx; i--){
21+
if(nums[i] > nums[idx]){
22+
swap(nums[i], nums[idx]);
23+
break;
24+
}
25+
}
26+
27+
reverse(nums.begin() + idx + 1, nums.end());
28+
29+
}
30+
};

0 commit comments

Comments
 (0)