We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
2 parents 31ef349 + 4c50c28 commit 80f23a5Copy full SHA for 80f23a5
Array/Next_Permutation.cpp
@@ -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
26
27
28
29
+ swap(nums[index1], nums[index2]);
30
+ reverse(nums.begin() + index1 + 1, nums.end());
31
32
33
+};
0 commit comments