Skip to content

Commit 7f085a5

Browse files
committed
1. Two Sum
1 parent 8da83d7 commit 7f085a5

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

Leetcode/0001.Two-Sum/README.md

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,33 @@ O(n)
2525

2626
## 來源
2727
* https://books.halfrost.com/leetcode/ChapterFour/0001~0099/0001.Two-Sum/
28-
* https://leetcode-cn.com/problems/two-sum/
28+
* https://leetcode-cn.com/problems/two-sum/
29+
30+
## 解答
31+
https://github.com/kimi0230/LeetcodeGolang/blob/master/Leetcode/0001.Two-Sum/twosum.go
32+
33+
```go
34+
package twosum
35+
36+
func Twosum(nums []int, target int) []int {
37+
for i, _ := range nums {
38+
for j := i + 1; j < len(nums); j++ {
39+
if target == nums[i]+nums[j] {
40+
return []int{i, j}
41+
}
42+
}
43+
}
44+
return []int{0, 0}
45+
}
46+
47+
func Twosum2(nums []int, target int) []int {
48+
m := make(map[int]int)
49+
for i, v := range nums {
50+
if idx, ok := m[target-v]; ok {
51+
return []int{idx, i}
52+
}
53+
m[v] = i
54+
}
55+
return []int{0, 0}
56+
}
57+
```

Leetcode/0322.Coin-Change/Coin-Change.go

Whitespace-only changes.

Leetcode/0322.Coin-Change/Coin-Change_test.go

Whitespace-only changes.

Leetcode/0322.Coin-Change/README.md

Whitespace-only changes.

0 commit comments

Comments
 (0)