forked from wufenggirl/LeetCode-in-Golang
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path4sum.go
executable file
·60 lines (52 loc) · 1.02 KB
/
4sum.go
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package problem0018
import (
"sort"
)
func fourSum(nums []int, target int) [][]int {
res := [][]int{}
sort.Ints(nums)
for i := 0; i < len(nums)-3; i++ {
// 避免添加重复的结果
// i>0 是为了防止nums[i-1]溢出
if i > 0 && nums[i] == nums[i-1] {
continue
}
for j := i + 1; j < len(nums)-2; j++ {
// 避免添加重复的结果
// nums[j-1]虽然不会溢出
// 但是不添加 j > i+1 的话,会漏掉那些i和j为相同数值的答案
if j > i+1 && nums[j] == nums[j-1] {
continue
}
l, r := j+1, len(nums)-1
for l < r {
s := nums[i] + nums[j] + nums[l] + nums[r]
switch {
case s < target:
l++
case s > target:
r--
default:
res = append(res, []int{nums[i], nums[j], nums[l], nums[r]})
l, r = next(nums, l, r)
}
}
}
}
return res
}
func next(nums []int, l, r int) (int, int) {
for l < r {
switch {
case nums[l] == nums[l+1]:
l++
case nums[r] == nums[r-1]:
r--
default:
l++
r--
return l, r
}
}
return l, r
}