-
Notifications
You must be signed in to change notification settings - Fork 0
/
4sum.go
51 lines (44 loc) · 940 Bytes
/
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
//https://leetcode.com/problems/4sum/
package leetcode_solutions_golang
import (
"fmt"
"sort"
)
func fourSum(nums []int, target int) [][]int {
sort.Ints(nums)
set := make(map[string][]int)
size := len(nums)
for i := 0; i < size; i++ {
for j := i + 1; j < size; j++ {
for k := j + 1; k < size; k++ {
comp := target - (nums[i] + nums[j] + nums[k])
if binarySearch(nums, comp, k+1, size-1) {
res := []int{nums[i], nums[j], nums[k], comp}
set[arrToString(res)] = res
}
}
}
}
result := make([][]int, 0)
for _, v := range set {
result = append(result, v)
}
return result
}
func arrToString(arr []int) string {
return fmt.Sprintf("%v", arr)
}
func binarySearch(arr []int, target, left, right int) bool {
for left <= right {
mid := (right + left) / 2
if arr[mid] == target {
return true
}
if arr[mid] > target {
right = mid - 1
} else {
left = mid + 1
}
}
return false
}