-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNext Permutation.java
More file actions
31 lines (31 loc) · 938 Bytes
/
Next Permutation.java
File metadata and controls
31 lines (31 loc) · 938 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
class Solution {
public void nextPermutation(int[] nums) {
int pNumIndex = -1;
for (int i = nums.length - 1; i >0; i --){
if (nums[i-1] < nums[i]){
pNumIndex = i-1;
break;
}
}
if (pNumIndex != -1){
int NumIndex = -1;
for (int i = nums.length - 1; i > pNumIndex; i --){
if (nums[i] > nums[pNumIndex]){
NumIndex = i;
break;
}
}
int tmp = nums[pNumIndex];
nums[pNumIndex] = nums[NumIndex];
nums[NumIndex] = tmp;
}
int start = pNumIndex + 1, end = nums.length -1;
while (start < end) {
int tmp = nums[start];
nums[start] = nums[end];
nums[end] = tmp;
start ++;
end --;
}
}
}