-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNextPermutation.java
More file actions
46 lines (41 loc) · 1.25 KB
/
NextPermutation.java
File metadata and controls
46 lines (41 loc) · 1.25 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package com.leetcode;
import java.util.Arrays;
/**
* Created by jamylu on 2018/1/18.
* leetcode031.
* 找出按字典序排序的下一个序列.如果是递减序列则返回排好序的递增序列
*/
public class NextPermutation {
public static void main(String[] args) {
int[] nums = {1, 5, 8, 4, 7, 6, 5, 3, 1};
next(nums);
for (int i : nums) {
System.out.print(i + " ");
}
}
public static void next(int[] nums) {
int i = nums.length - 2;
// 从后往前找出第一个递减的数的下标
while (i >= 0 && nums[i] >= nums[i + 1]) {
i--;
}
if (i >= 0) {
int j = i + 1;
int min = nums[j];
int flag = j;
// 找出在i之后比nums[i]大的最小数
for (; j <= nums.length - 1; j++) {
if (nums[j] > nums[i] && min > nums[j]) {
min = nums[j];
flag = j;
}
}
// 交换nums[i]和nums[j]
int temp = nums[i];
nums[i] = nums[flag];
nums[flag] = temp;
}
// 重排nums[i+1]到nums[len-1]为递增序列
Arrays.sort(nums, i + 1, nums.length);
}
}