-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path15. 3Sum.go
97 lines (87 loc) · 1.99 KB
/
15. 3Sum.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package main
import (
// "fmt"
"sort"
)
func main() {
}
// func threeSum(nums []int) [][]int {
// if len(nums) < 3 {
// return [][]int{{}}
// }
// // for i := 0; i < len(nums); i++ {
// // numMap[nums[i]] = i
// // }
// sort.Ints(nums)
// var ans [][]int
// for i := 0; i < len(nums)-2; i++ {
// fmt.Println("🚀 ~ file: 15. 3Sum.go ~ line 24 ~ fori:=0;i<len ~ nums[i] : ", nums[i])
// if i > 0 && nums[i] == nums[i-1] {
// continue
// }
// numMap:= map[int]int{}
// for j := i + 1; j < len(nums); j++ {
// if _, ok := numMap[-nums[i]-nums[j]]; ok {
// ans = append(ans, []int{nums[i], nums[j], -nums[i] - nums[j]})
// } else {
// numMap[nums[j]] = j
// }
// fmt.Println("🚀 ~ file: 15. 3Sum.go ~ line 31 ~ forj:=i+1;j<len ~ ans : ", j, ans)
// }
// }
// return ans
// }
func threeSum(nums []int) [][]int {
sort.Ints(nums)
var result [][]int
m := make(map[[3]int]struct{})
for i := 1; i < len(nums)-1; i++ {
start := 0
end := len(nums) - 1
for start < i && end > i {
if nums[start]+nums[end] < 0-nums[i] {
start++
} else if nums[start]+nums[end] > 0-nums[i] {
end--
} else {
curr := [3]int{nums[start], nums[i], nums[end]}
if _, ok := m[curr]; !ok {
m[curr] = struct{}{}
result = append(result, curr[:])
}
start++
end--
}
}
}
return result
}
func threeSum(nums []int) [][]int {
var results [][]int
sort.Ints(nums)
for i := 0; i < len(nums)-2; i++ {
if i > 0 && nums[i] == nums[i-1] {
continue//To prevent the repeat
}
target, left, right := -nums[i], i+1, len(nums)-1
for left < right {
sum := nums[left] + nums[right]
if sum == target {
results = append(results, []int{nums[i], nums[left], nums[right]})
left++
right--
for left < right && nums[left] == nums[left-1] {
left++
}
for left < right && nums[right] == nums[right+1] {
right--
}
} else if sum > target {
right--
} else if sum < target {
left++
}
}
}
return results
}