-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy paththreeSum.go
More file actions
49 lines (41 loc) · 1020 Bytes
/
Copy paththreeSum.go
File metadata and controls
49 lines (41 loc) · 1020 Bytes
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
/* https://leetcode.com/problems/3sum/#/description
Given an array S of n integers, are there elements a, b, c in S such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero.
Note: The solution set must not contain duplicate triplets.
For example, given array S = [-1, 0, 1, 2, -1, -4],
A solution set is:
[
[-1, 0, 1],
[-1, -1, 2]
]
*/
package larray
import "sort"
func threeSum(nums []int) [][]int {
sort.Ints(nums)
result := [][]int{}
for i := 0; i+2 < len(nums); i++ {
if i > 0 && nums[i] == nums[i-1] { // skip same result
continue
}
j, k := i+1, len(nums)-1
target := -nums[i]
for j < k {
if temp := nums[j] + nums[k]; temp == target {
result = append(result, []int{nums[i], nums[j], nums[k]})
j++
k--
for j < k && nums[j] == nums[j-1] {
j++ // skip same result
}
for j < k && nums[k] == nums[k+1] {
k-- // skip same result
}
} else if temp > target {
k--
} else {
j++
}
}
}
return result
}