Skip to content

Commit

Permalink
Create Next Permutation.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
liuyl committed Feb 25, 2013
1 parent dc9acbe commit 44dcd53
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Next Permutation.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
void nextPermutation(vector<int> &num) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int len = num.size();
int ind = -1, ind2;
for (int i = len - 1; i != 0; i--){
if (num[i-1] < num[i]){
ind = i - 1;
break;
}
}
if (ind == -1){
sort(num.begin(), num.end());
}else{
for (int i = len - 1; i != ind; i--){
if (num[i] > num[ind]){
ind2 = i;
break;
}
}
int tmp = num[ind2];
num[ind2] = num[ind];
num[ind] = tmp;
sort(num.begin() + ind+1, num.end());
}
return ;
}
};

0 comments on commit 44dcd53

Please sign in to comment.