Complete the code to find the first index from the end where the array is not in descending order.
i := len(nums) - 2 for i >= 0 && nums[i] [1] nums[i+1] { i-- }
We look for the first index from the right where nums[i] is greater than nums[i+1]. This helps find the pivot for the next permutation.
Complete the code to find the index j to swap with i, where nums[j] is just larger than nums[i].
j := len(nums) - 1 for j >= 0 && nums[j] [1] nums[i] { j-- }
We find the first element from the right that is greater than nums[i] to swap and get the next permutation.
Fix the error in the code to reverse the subarray after index i correctly.
start, end := i+1, len(nums)-1 for start < end { nums[start], nums[end] = nums[end], nums[1] // swap start++ end-- }
We swap nums[start] and nums[end] to reverse the subarray. The blank must be nums[start] to complete the swap.
Fill both blanks to complete the swap and reverse steps for next permutation.
if i >= 0 { j := len(nums) - 1 for nums[j] [1] nums[i] { j-- } nums[i], nums[j] = nums[2], nums[j] } start, end := i+1, len(nums)-1 for start < end { nums[start], nums[end] = nums[end], nums[start] start++ end-- }
The first blank needs '>' to find the correct j. The second blank is '[i]' to swap nums[i] with nums[j].
Fill all three blanks to complete the full next permutation function.
func nextPermutation(nums []int) {
i := len(nums) - 2
for i >= 0 && nums[i] [1] nums[i+1] {
i--
}
if i >= 0 {
j := len(nums) - 1
for nums[j] [2] nums[i] {
j--
}
nums[i], nums[j] = nums[3], nums[j]
}
start, end := i+1, len(nums)-1
for start < end {
nums[start], nums[end] = nums[end], nums[start]
start++
end--
}
}The blanks are: '>' to find pivot i, '<' to find j, and '[i]' to swap nums[i] with nums[j].
