-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path15-3Sum.go
More file actions
152 lines (137 loc) · 4.98 KB
/
15-3Sum.go
File metadata and controls
152 lines (137 loc) · 4.98 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package main
// 15. 3Sum
// Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
// Notice that the solution set must not contain duplicate triplets.
// Example 1:
// Input: nums = [-1,0,1,2,-1,-4]
// Output: [[-1,-1,2],[-1,0,1]]
// Explanation:
// nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
// nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
// nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
// The distinct triplets are [-1,0,1] and [-1,-1,2].
// Notice that the order of the output and the order of the triplets does not matter.
// Example 2:
// Input: nums = [0,1,1]
// Output: []
// Explanation: The only possible triplet does not sum up to 0.
// Example 3:
// Input: nums = [0,0,0]
// Output: [[0,0,0]]
// Explanation: The only possible triplet sums up to 0.
// Constraints:
// 3 <= nums.length <= 3000
// -10^5 <= nums[i] <= 10^5
import "fmt"
import "sort"
// 最优解,双指针 + 排序
func threeSum(nums []int) [][]int {
sort.Ints(nums)
res, start, end, index, addNum, length := make([][]int, 0), 0, 0, 0, 0, len(nums)
for index = 1; index < length-1; index++ {
start, end = 0, length-1
if index > 1 && nums[index] == nums[index-1] {
start = index - 1
}
for start < index && end > index {
if start > 0 && nums[start] == nums[start-1] {
start++
continue
}
if end < length-1 && nums[end] == nums[end+1] {
end--
continue
}
addNum = nums[start] + nums[end] + nums[index]
if addNum == 0 {
res = append(res, []int{nums[start], nums[index], nums[end]})
start++
end--
} else if addNum > 0 {
end--
} else {
start++
}
}
}
return res
}
func threeSum1(nums []int) [][]int {
res := [][]int{}
counter := map[int]int{}
for _, value := range nums {
counter[value]++
}
uniqNums := []int{}
for key := range counter {
uniqNums = append(uniqNums, key)
}
sort.Ints(uniqNums)
for i := 0; i < len(uniqNums); i++ {
if (uniqNums[i]*3 == 0) && counter[uniqNums[i]] >= 3 {
res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[i]})
}
for j := i + 1; j < len(uniqNums); j++ {
if (uniqNums[i]*2+uniqNums[j] == 0) && counter[uniqNums[i]] > 1 {
res = append(res, []int{uniqNums[i], uniqNums[i], uniqNums[j]})
}
if (uniqNums[j]*2+uniqNums[i] == 0) && counter[uniqNums[j]] > 1 {
res = append(res, []int{uniqNums[i], uniqNums[j], uniqNums[j]})
}
c := 0 - uniqNums[i] - uniqNums[j]
if c > uniqNums[j] && counter[c] > 0 {
res = append(res, []int{uniqNums[i], uniqNums[j], c})
}
}
}
return res
}
// best solution
func threeSum2(nums []int) [][]int {
var res [][]int
if len(nums) < 3 {
return res
}
sort.Ints(nums)
for i := 0; i < len(nums)-1; i++ {
if i != 0 && nums[i-1] == nums[i] {
continue
}
for k, j := i+1, len(nums)-1; k < j; {
n := nums[i] + nums[k] + nums[j]
if n == 0 {
res = append(res, []int{nums[i], nums[k], nums[j]})
p := k
for p < j && nums[p] == nums[k] {
p++
}
k = p
} else if n > 0 {
j--
} else {
k++
}
}
}
return res
}
func main() {
// fmt.Printf("threeSum([]int{-1, 0, 1, 2, -1, -4}) = %v\n",threeSum([]int{-1, 0, 1, 2, -1, -4}))
// fmt.Printf("threeSum1([]int{-1, 0, 1, 2, -1, -4}) = %v\n",threeSum1([]int{-1, 0, 1, 2, -1, -4}))
// fmt.Printf("threeSum2([]int{-1, 0, 1, 2, -1, -4}) = %v\n",threeSum2([]int{-1, 0, 1, 2, -1, -4}))
// nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
// nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
// nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
fmt.Println(threeSum([]int{-1, 0, 1, 2, -1, -4})) // [[-1,-1,2],[-1,0,1]]
fmt.Println(threeSum([]int{0,1,1})) // []
fmt.Println(threeSum([]int{0,0,0})) // [[0,0,0]]
fmt.Println(threeSum([]int{0,1,1,-2,3,2,-1})) // [[-2 -1 3] [-2 0 2] [-1 0 1] [-2 1 1]]
fmt.Println(threeSum1([]int{-1, 0, 1, 2, -1, -4})) // [[-1,-1,2],[-1,0,1]]
fmt.Println(threeSum1([]int{0,1,1})) // []
fmt.Println(threeSum1([]int{0,0,0})) // [[0,0,0]]
fmt.Println(threeSum1([]int{0,1,1,-2,3,2,-1})) // [[-2 -1 3] [-2 0 2] [-1 0 1] [-2 1 1]]
fmt.Println(threeSum2([]int{-1, 0, 1, 2, -1, -4})) // [[-1,-1,2],[-1,0,1]]
fmt.Println(threeSum2([]int{0,1,1})) // []
fmt.Println(threeSum2([]int{0,0,0})) // [[0,0,0]]
fmt.Println(threeSum2([]int{0,1,1,-2,3,2,-1})) // [[-2 -1 3] [-2 0 2] [-1 0 1] [-2 1 1]]
}