8000 更改 problem 0001 · DontFearAlgorithms/LeetCode-Go@9e03e1a · GitHub
[go: up one dir, main page]

Skip to content

Commit 9e03e1a

Browse files
committed
更改 problem 0001
1 parent 18d02ad commit 9e03e1a

File tree

4 files changed

+47
-122
lines changed

4 files changed

+47
-122
lines changed

Algorithms/0001. Two Sum/1. Add Two Numbers.go

Lines changed: 0 additions & 43 deletions
This file was deleted.

Algorithms/0001. Two Sum/1. Add Two Numbers_test.go

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package leetcode
2+
3+
func twoSum(nums []int, target int) []int {
4+
m := make(map[int]int)
5+
for i := 0; i < len(nums); i++ {
6+
another := target - nums[i]
7+
if _, ok := m[another]; ok {
8+
return []int{m[another], i}
9+
}
10+
m[nums[i]] = i
11+
}
12+
return nil
13+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
func TestTwoSum(t *testing.T) {
9+
tests := [][]int{
10+
[]int{3, 2, 4},
11+
[]int{3, 2, 4},
12+
[]int{0, 8, 7, 3, 3, 4, 2},
13+
[]int{0, 1},
14+
}
15+
targets := []int{
16+
6,
17+
5,
18+
11,
19+
1,
20+
}
21+
results := [][]int{
22+
[]int{1, 2},
23+
[]int{0, 1},
24+
[]int{1, 3},
25+
[]int{0, 1},
26+
}
27+
fmt.Printf("------------------------Leetcode Problem 1------------------------\n")
28+
for i := 0; i < len(targets); i++ {
29+
fmt.Printf("nums = %v target = %v result = %v\n", tests[i], targets[i], twoSum(tests[i], targets[i]))
30+
if ret := twoSum(tests[i], targets[i]); ret[0] != results[i][0] && ret[1] != results[i][1] {
31+
t.Fatalf("case %d fails: %v\n", i, ret)
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)
0