Skip to content

Commit 2c7f9e6

Browse files
Create 31. Next Permutation
1 parent 4fbcc35 commit 2c7f9e6

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

31. Next Permutation

+39
Original file line numberDiff line numberDiff line change
@@ -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+
break;
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

Comments
 (0)