Skip to content

Commit 3e934e8

Browse files
committed
add-construct-about-hashMap
1 parent a76c7e5 commit 3e934e8

File tree

5 files changed

+95
-19
lines changed

5 files changed

+95
-19
lines changed

15.3Sum/main.go

+60-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,63 @@
1-
package threeSum
1+
package main
2+
3+
import (
4+
"fmt"
5+
"sort"
6+
)
27

38
func threeSum(nums []int) [][]int {
4-
return [][]int{}
9+
sort.Ints(nums) // 排序函数
10+
res := [][]int{}
11+
if len(nums) < 3 {
12+
return res
13+
}
14+
15+
for i := 1; i < len(nums)-1; i++ {
16+
if i > 0 && nums[i] == nums[i-1] {
17+
continue
18+
}
19+
l, r := 0, len(nums)-1
20+
for l < r {
21+
sum := nums[l] + nums[i] + nums[r]
22+
fmt.Println(sum, l, r)
23+
switch {
24+
case sum > 0:
25+
r--
26+
case sum < 0:
27+
l++
28+
default:
29+
res = append(res, []int{nums[l], nums[i], nums[r]})
30+
// 此处为了防止重复, `0,0,0,1,0,0,1`这种情况
31+
l, r = next(nums, l, r)
32+
}
33+
}
34+
}
35+
36+
return res
37+
}
38+
39+
func next(nums []int, l, r int) (int, int) {
40+
switch {
41+
case nums[l] == nums[l+1]:
42+
l++
43+
case nums[r] == nums[r-1]:
44+
r--
45+
default:
46+
l++
47+
r--
48+
}
49+
return l, r
50+
}
51+
52+
// func indexOf(element int, data []int) int {
53+
// for k, v := range data {
54+
// if element == v {
55+
// return k
56+
// }
57+
// }
58+
// return -1 //not found.
59+
// }
60+
61+
func main() {
62+
fmt.Println(threeSum([]int{4, 4, 3, -5, 0, 0, 0, -2, 3, -5, -5, 0}))
563
}

15.3Sum/main_test.go

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package threeSum
1+
package main
22

33
import (
44
"testing"
@@ -32,7 +32,34 @@ func Test_OK(t *testing.T) {
3232
a: ans{
3333
one: [][]int{
3434
[]int{-1, 0, 1},
35-
[]int{-1, 0, 1},
35+
[]int{-1, -1, 2},
36+
},
37+
},
38+
},
39+
40+
question{
41+
p: para{
42+
one: []int{-4, -2, 1, -5, -4, -4, 4, -2, 0, 4, 0, -2, 3, 1, -5, 0},
43+
},
44+
a: ans{
45+
one: [][]int{
46+
[]int{-4, 1, 3},
47+
[]int{-4, 0, 4},
48+
[]int{-2, 1, 1},
49+
[]int{-2, -2, 4},
50+
[]int{-5, 1, 4},
51+
[]int{0, 0, 0},
52+
},
53+
},
54+
},
55+
56+
question{
57+
p: para{
58+
one: []int{0, 0, 0, 0},
59+
},
60+
a: ans{
61+
one: [][]int{
62+
[]int{0, 0, 0},
3663
},
3764
},
3865
},

codecov.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
coverage:
2-
status:
3-
project:
4-
users:
5-
paths:
6-
- 1.TwoSum/
1+
# coverage:
2+
# status:
3+
# project:
4+
# users:
5+
# paths:
6+
# - 1.TwoSum/

go_tutorail.go goTutorail.go

File renamed without changes.

index.js

-9
This file was deleted.

0 commit comments

Comments
 (0)