diff --git a/two_sum.go b/two_sum.go index d355ee49..534287cb 100755 --- a/two_sum.go +++ b/two_sum.go @@ -1,605 +1,13 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - go-leetcode/two_sum.go at master · hitzzc/go-leetcode · GitHub - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Skip to content -
- - - -
-
-
- -
Learn more - -
Please note that GitHub no longer supports your web browser.
-

We recommend upgrading to the latest Google Chrome or Firefox.

-
-
- - - - - - - -
- -
- -
-
- - - -
-
-
- - - - - -
-
- - - -

- - /go-leetcode - -

- -
- -
- -
-
- - - Permalink - - - -
- -
- - -
- -
-
- - Switch branches/tags -
- -
-
- -
-
- -
-
- - - -
- - -
Nothing to show
-
- -
-
-
- -
- - Find file - - -
- -
- - - -
- - - 2b7a590 - - Sep 9, 2016 - -
- - zczou - two_sum -
- -
- - -
- - -
- -
-
-
- -
- Raw - Blame - History -
- - - - -
- -
- 14 lines (12 sloc) - - 249 Bytes -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
package two_sum
-
func twoSum(nums []int, target int) []int {
m := make(map[int]int)
for i := 0; i < len(nums); i++ {
another := target - nums[i]
if _, ok := m[another]; ok {
return []int{m[another], i}
}
m[nums[i]] = i
}
return nil
}
- - -
- -
- - - - -
- -
- -
-
- -
- - - - - - -
- - - You can't perform that action at this time. -
- - -
- - You signed in with another tab or window. Reload to refresh your session. - You signed out in another tab or window. Reload to refresh your session. -
- - - - - - +package two_sum + +func twoSum(nums []int, target int) []int { + m := make(map[int]int) + for i := 0; i < len(nums); i++ { + another := target - nums[i] + if _, ok := m[another]; ok { + return []int{m[another], i} + } + m[nums[i]] = i + } + return nil +} diff --git a/two_sum_test.go b/two_sum_test.go index 2a076086..a75256fc 100755 --- a/two_sum_test.go +++ b/two_sum_test.go @@ -1,651 +1,29 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - go-leetcode/two_sum_test.go at master · hitzzc/go-leetcode · GitHub - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- Skip to content -
- - - -
-
-
- -
Learn more - -
Please note that GitHub no longer supports your web browser.
-

We recommend upgrading to the latest Google Chrome or Firefox.

-
-
- - - - - - - -
- -
- -
-
- - - -
-
-
- - - - - -
-
- - - -

- - /go-leetcode - -

- -
- -
- -
-
- - - Permalink - - - -
- -
- - -
- -
-
- - Switch branches/tags -
- -
-
- -
-
- -
-
- - - -
- - -
Nothing to show
-
- -
-
-
- -
- - Find file - - -
- -
- - - -
- Fetching contributors… -
- -
- - Cannot retrieve contributors at this time -
-
-
-
-
- -
- Raw - Blame - History -
- - - - -
- -
- 30 lines (27 sloc) - - 457 Bytes -
-
- - - -
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
package two_sum
-
import (
"testing"
)
-
func TestTwoSum(t *testing.T) {
tests := [][]int{
[]int{3, 2, 4},
[]int{0, 8, 7, 3, 3, 4, 2},
[]int{0, 1},
}
targets := []int{
6,
11,
1,
}
results := [][]int{
[]int{1, 2},
[]int{1, 3},
[]int{0, 1},
}
caseNum := 3
for i := 0; i < caseNum; i++ {
if ret := twoSum(tests[i], targets[i]); ret[0] != results[i][0] && ret[1] != results[i][1] {
t.Fatalf("case %d fails: %v\n", i, ret)
}
}
}
- - -
- -
- - - - -
- -
- -
-
- -
- - - - - - -
- - - You can't perform that action at this time. -
- - -
- - You signed in with another tab or window. Reload to refresh your session. - You signed out in another tab or window. Reload to refresh your session. -
- - - - - - +package two_sum + +import ( + "testing" +) + +func TestTwoSum(t *testing.T) { + tests := [][]int{ + []int{3, 2, 4}, + []int{0, 8, 7, 3, 3, 4, 2}, + []int{0, 1}, + } + targets := []int{ + 6, + 11, + 1, + } + results := [][]int{ + []int{1, 2}, + []int{1, 3}, + []int{0, 1}, + } + caseNum := 3 + for i := 0; i < caseNum; i++ { + if ret := twoSum(tests[i], targets[i]); ret[0] != results[i][0] && ret[1] != results[i][1] { + t.Fatalf("case %d fails: %v\n", i, ret) + } + } +}