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
| // @Title: 下一个排列 (Next Permutation)
// @Author: 15816537946@163.com
// @Date: 2020-11-03 23:31:05
// @Runtime: 4 ms
// @Memory: 2.5 MB
func nextPermutation(nums []int) {
for i := len(nums) - 1; i > 0; i-- {
if nums[i-1] < nums[i] {
minLen := i
for j := i; j < len(nums); j++ {
if nums[i-1] < nums[j] && nums[j] < nums[minLen] {
minLen = j
}
}
nums[i-1], nums[minLen] = nums[minLen], nums[i-1]
sort.Ints(nums[i:])
return
}
}
sort.Ints(nums)
return
}
/*
3步:
1. 从右往左找到第一个比当前小的数字
2. 找到后, 再从左往右找到比这个数大的最小数
3. 然后 swap
4. 对右侧的数组从小按大排序
*/
|