-
Notifications
You must be signed in to change notification settings - Fork 0
/
018.go
60 lines (53 loc) · 1.34 KB
/
018.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 p018
import (
"sort"
)
/**
Given an array S of n integers, are there elements a, b, c, and d in S such that a + b + c + d = target? Find all unique quadruplets in the array which gives the sum of target.
Note: The solution set must not contain duplicate quadruplets.
For example, given array S = [1, 0, -1, 0, -2, 2], and target = 0.
A solution set is:
[
[-1, 0, 0, 1],
[-2, -1, 1, 2],
[-2, 0, 0, 2]
]
*/
func fourSum(nums []int, target int) [][]int {
ansSet := make([][]int, 0)
sort.Ints(nums)
var lastAns []int
for head := 0; head < len(nums)-3; head++ {
if lastAns != nil && nums[head] == lastAns[0] {
continue
}
lastAns = nil
tmh := target - nums[head]
for first := head + 1; first < len(nums)-2; first++ {
if lastAns != nil && nums[first] == lastAns[1] {
continue
}
tmhs := tmh - nums[first]
second := first + 1
third := len(nums) - 1
for second < third {
curSum := nums[second] + nums[third]
if curSum == tmhs { // one ans
lastAns = []int{nums[head], nums[first], nums[second], nums[third]}
ansSet = append(ansSet, lastAns)
for second < third && nums[second] == lastAns[2] {
second++
}
for third > second && nums[third] == lastAns[3] {
third--
}
} else if curSum < tmhs {
second++
} else {
third--
}
}
}
}
return ansSet
}