We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 7fc0f3c commit 5f3d828Copy full SHA for 5f3d828
go/next_permutation.go
@@ -6,15 +6,13 @@ import "sort"
6
func nextPermutation(nums []int) {
7
for i := len(nums) - 1; i > 0; i-- {
8
if nums[i-1] < nums[i] {
9
- minN := nums[i]
10
- idx := i
+ candidate, candidateIndex := nums[i], i
11
for j := i; j < len(nums); j++ {
12
- if nums[j] < minN && nums[i-1] < nums[j] {
13
- idx = j
14
- minN = nums[j]
+ if nums[j] < candidate && nums[i-1] < nums[j] {
+ candidate, candidateIndex = nums[j], j
15
}
16
17
- nums[i-1], nums[idx] = nums[idx], nums[i-1]
+ nums[i-1], nums[candidateIndex] = nums[candidateIndex], nums[i-1]
18
sort.Slice(nums[i:], func(a, b int) bool {
19
return nums[i+a] < nums[i+b]
20
})
0 commit comments