Skip to content

Commit 80f23a5

Browse files
Merge pull request #17 from amitShindeGit/amitShindeBranch
Added next permutation soln in c++
2 parents 31ef349 + 4c50c28 commit 80f23a5

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

Diff for: Array/Next_Permutation.cpp

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
void swap(int &x, int &y){
4+
int temp = x;
5+
x = y;
6+
y = temp;
7+
}
8+
9+
void nextPermutation(vector<int>& nums) {
10+
int size = nums.size();
11+
int index1;
12+
int index2;
13+
14+
for(index1=size-2; index1 >= 0; index1--){
15+
if(nums[index1] < nums[index1 + 1]){ //we will find our index1 here
16+
break;
17+
}
18+
}
19+
20+
if(index1 < 0){
21+
reverse(nums.begin(), nums.end());
22+
}else{
23+
for(index2 = size-1; index2 > index1; index2--){
24+
if(nums[index2] > nums[index1]){ //we will find our index2 here
25+
break;
26+
}
27+
}
28+
29+
swap(nums[index1], nums[index2]);
30+
reverse(nums.begin() + index1 + 1, nums.end());
31+
}
32+
}
33+
};

0 commit comments

Comments
 (0)