-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path18.四数之和.go
61 lines (52 loc) · 1.31 KB
/
18.四数之和.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
61
/*
* @lc app=leetcode.cn id=18 lang=golang
*
* [18] 四数之和
*/
// @lc code=start
package main
import (
"fmt"
"sort"
)
func fourSum(nums []int, target int) [][]int {
sort.Ints(nums)
n := len(nums)
res := [][]int{}
for first := 0 ; first < n - 3 ; first++ {
if first > 0 && nums[first] == nums[first-1] {
continue
}
for second := first + 1 ; second < n - 2 ; second++{
if second > first + 1 && nums[second] == nums[second - 1] {
continue
}
for third := second + 1 ; third < n -1 ; third++ {
if third > second + 1 && nums[third] == nums[third - 1] {
continue
}
left := target - nums[first] - nums[second] - nums[third]
fourth := n - 1
for nums[fourth] > left && fourth > third {
fourth--
}
if fourth == third {
continue
}
if nums[fourth] + nums[first] + nums[second] + nums[third] == target {
res = append(res, []int{nums[first],nums[second], nums[third], nums[fourth]})
}
}
}
}
return res
}
// @lc code=end
func main() {
a := []int{1, 0, -1, 0, -2, 2}
fmt.Printf("original are %v, res is %v\n", a, fourSum(a, 0 ))
b := []int{3,0,-2,-1,1,2}
fmt.Printf("original are %v, res is %v\n", b, fourSum(b, 0))
c := []int{-4,-2,1,-5,-4,-4,4,-2,0,4,0,-2,3,1,-5,0}
fmt.Printf("original are %v, res is %v\n", c, fourSum(c, 0))
}