We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b97d907 commit 0172de0Copy full SHA for 0172de0
next-permutation/next-permutation.cpp
@@ -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
24
25
26
27
+ reverse(nums.begin() + idx + 1, nums.end());
28
29
30
+};
0 commit comments