-
Notifications
You must be signed in to change notification settings - Fork 20
/
FourSum.go
67 lines (60 loc) · 1.22 KB
/
FourSum.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
62
63
64
65
66
67
package FourSum
import "sort"
func fourSum(nums []int, target int) [][]int {
results := make([][]int, 0)
sort.Ints(nums)
numsLength := len(nums)
for i := 0; i < numsLength-3; i++ {
for j := i + 1; j < numsLength-2; j++ {
left, right := j+1, numsLength-1
for ; left < right; {
sum := nums[i] + nums[j] + nums[left] + nums[right]
if sum == target {
tmp := []int{nums[i], nums[j], nums[left], nums[right]}
exits := false
for _, l := range results {
if sameIntSlice(l, tmp) {
exits = true
break
}
}
if !exits {
results = append(results, tmp)
}
left++
right--
} else if sum < target {
left++
} else {
right--
}
}
}
}
return results;
}
func sameIntSlice(x, y []int) bool {
if len(x) != len(y) {
return false
}
// create a map of int -> int
diff := make(map[int]int, len(x))
for _, xx := range x {
// 0 value for int is 0, so just increment a counter for the int
diff[xx]++
}
for _, yy := range y {
// If the string yy is not in diff bail out early
if _, ok := diff[yy]; !ok {
return false
}
diff[yy] -= 1
if diff[yy] == 0 {
delete(diff, yy)
}
}
if len(diff) == 0 {
return true
}
return false
}