File tree Expand file tree Collapse file tree 4 files changed +30
-1
lines changed Expand file tree Collapse file tree 4 files changed +30
-1
lines changed Original file line number Diff line number Diff line change 25
25
26
26
## 來源
27
27
* 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
+ ```
You can’t perform that action at this time.
0 commit comments