-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution31.java
More file actions
executable file
·30 lines (30 loc) · 1.05 KB
/
Copy pathSolution31.java
File metadata and controls
executable file
·30 lines (30 loc) · 1.05 KB
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
class Solution31 {
public void nextPermutation(int[] nums) {
int index = nums.length - 1, index2 = 0, temp = 1;
int begin = 0, end = nums.length - 1;
// 找到中间最大值,并记录下标
for(int i = nums.length - 1; i >= 0; i--)
if ( i != 0 && nums[i - 1] < nums[i]) {index = i; temp = 0; break;}
// 从右边找大值
if (index != nums.length - 1 || (index == nums.length - 1 && temp == 0)){
for(int i = nums.length - 1; i >= 0; i--)
if (nums[i] > nums[index - 1]) {index2 = i; break;}
// 交换
temp = nums[index - 1];
nums[index - 1] = nums[index2];
nums[index2] = temp;
begin = index;
}
// 排序
for (int i = 0; i < nums.length; i++){
if (begin >= end) break;
temp = nums[end];
nums[end] = nums[begin];
nums[begin] = temp;
begin++;
end--;
}
for (int a : nums)
System.out.println(a);
}
}