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 b94b4ad commit 62db505Copy full SHA for 62db505
cpp/_31.cpp
@@ -0,0 +1,33 @@
1
+class Solution {
2
+public:
3
+ void nextPermutation(vector<int>& nums) {
4
+
5
+ int i1,i2;
6
+ bool hasPermutation = false;
7
+ for(int i=nums.size()-1;i>0;i--){
8
+ if(nums[i-1]<nums[i]){
9
+ i1 = i-1;
10
+ i2 = i;
11
+ hasPermutation = true;
12
+ break;
13
+ }
14
15
16
+ if(hasPermutation){
17
+ int j=i2;
18
+ for(int i=nums.size()-1;i>i1;i--){
19
+ if(nums[i]>nums[i1]){
20
+ j=i;
21
22
23
24
+ swap(nums[i1],nums[j]);
25
+ reverse(nums.begin()+i1+1,nums.end());
26
+ }else{
27
+ sort(nums.begin(),nums.end());
28
29
+ for(auto i:nums){
30
+ cout << i << " ";
31
32
33
+};
0 commit comments