Open
Description
复制到公众号文章时,没有滚动条,且会自动换行如下
代码如下
package main
import (
"fmt"
"sort"
)
func main() {
nums := []int{1, 0, -1, 0, -2, 2}
fmt.Println(fourSum(nums, 0))
}
// https://leetcode.cn/problems/4sum/description/
func fourSum(nums []int, target int) [][]int {
if len(nums) < 4 {
return nil
}
res := make([][]int, 0)
sort.Ints(nums)
for i := 0; i < len(nums)-3; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue
}
for j := i + 1; j < len(nums)-2; j++ {
if j > i+1 && nums[j] == nums[j-1] {
continue
}
left, right := j+1, len(nums)-1
for left < right {
sum := nums[i] + nums[j] + nums[left] + nums[right]
if sum == target {
res = append(res, []int{nums[i], nums[j], nums[left], nums[right]})
for left < right && nums[left] == nums[left+1] {
left++
}
for left < right && nums[right] == nums[right-1] {
right--
}
left++
} else if sum < target {
left++
} else {
right--
}
}
}
}
return res
}
这里粘贴的代码缩进只是一个TAB....
其他建议:代码块,代码语法缩进需要调整
Metadata
Assignees
Labels
No labels