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 7ab6311 commit 7fc0f3cCopy full SHA for 7fc0f3c
go/next_permutation.go
@@ -0,0 +1,27 @@
1
+//lint:file-ignore U1000 Ignore all unused code
2
+package main
3
+
4
+import "sort"
5
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
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]
15
+ }
16
17
+ nums[i-1], nums[idx] = nums[idx], nums[i-1]
18
+ sort.Slice(nums[i:], func(a, b int) bool {
19
+ return nums[i+a] < nums[i+b]
20
+ })
21
+ return
22
23
24
+ sort.Slice(nums, func(a, b int) bool {
25
+ return nums[a] < nums[b]
26
27
+}
0 commit comments