-
Notifications
You must be signed in to change notification settings - Fork 0
/
31.下一个排列.java
62 lines (52 loc) · 1.63 KB
/
31.下一个排列.java
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* @lc app=leetcode.cn id=31 lang=java
*
* [31] 下一个排列
*/
// @lc code=start
class Solution {
public void nextPermutation(int[] nums) {
int len = nums.length;
if(len == 0 || len == 1){
return;
}
int target = findTarget(nums);//从右到左,找到第一个比上一个小的数a[i]
if(target == -1){
reverse(nums, 0, len-1);//找不到,则说明没有符合条件的下一个排列了,直接反转
return;
}
adjustTarget(nums, target);//从右到左,找到第一个比a[i]大的数,与a[i]交换位置
reverse(nums, target+1, len-1);//将i+1后面的数反转一下,使之从小到大
}
private void reverse(int[] nums, int start, int end){
while(start < end){
swap(start, end, nums);
start++;
end--;
}
}
private void adjustTarget(int[] nums, int targetIndex){//1
int len = nums.length;
for(int i = len-1; i >= 0; i--){
if(nums[targetIndex] < nums[i]){//nums[1]<nums[2]
swap(targetIndex, i, nums);//targetIndex = 1, i = 2
break;
}
}
}
private void swap(int swapIndex1, int swapIndex2, int[] nums){
int temp = nums[swapIndex1];
nums[swapIndex1] = nums[swapIndex2];
nums[swapIndex2] = temp;
}
private int findTarget(int[] nums){
int len = nums.length;
for(int i = len-1; i > 0; i--){
if(nums[i-1] < nums[i]){
return i-1;
}
}
return -1;
}
}
// @lc code=end