We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 4fbcc35 commit 2c7f9e6Copy full SHA for 2c7f9e6
31. Next Permutation
@@ -0,0 +1,39 @@
1
+class Solution {
2
+public:
3
+ void nextPermutation(vector<int>& nums) {
4
+ int i=nums.size()-1;
5
+ int j=nums.size()-1;
6
+ int p=0,q=0;
7
+ for(int k=i; k>0; k--)
8
+ {
9
+ if(nums[k-1]<nums[k])
10
11
+ i=k;
12
+ p++;
13
+ break;
14
+ }
15
+
16
17
+ if(p==0)
18
19
+ reverse(nums.begin(),nums.end());
20
+ return;
21
22
+ for(int k=j; k>0; k--)
23
24
+ if(nums[i-1]<nums[k])
25
26
+ j=k;
27
+ q++;
28
29
30
31
32
33
34
+ swap(nums[i-1],nums[j]);
35
+ reverse(nums.begin()+i,nums.end());
36
37
38
39
+};
0 commit comments