-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path31. Next Permutation.java
More file actions
35 lines (34 loc) · 953 Bytes
/
Copy path31. Next Permutation.java
File metadata and controls
35 lines (34 loc) · 953 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
32
33
34
35
public class Solution {
public void nextPermutation(int[] nums) {
// The idea is pretty simple, scan from right to left
// find the first element that breaking the increasing trend
// and find the first element that greater than the breaking item
// swap them and reverse the rest part
int len = nums.length;
int i = len - 2;
for (; i >= 0; i--) {
if (nums[i + 1] > nums[i]) {
break;
}
}
int r = len - 1;
if (i >= 0) {
for (; r >= 0; r--) {
if (nums[r] > nums[i]) {
break;
}
}
swap(nums, r, i);
}
i++;
r = len - 1;
while (i < r) {
swap(nums, i++, r--);
}
}
public void swap(int[] nums, int i, int j) {
int tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
}