mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-09 03:34:02 +08:00
添加 0001.两数之和 go版
使用map方式解题,降低时间复杂度
This commit is contained in:
@ -134,6 +134,20 @@ func twoSum(nums []int, target int) []int {
|
|||||||
}
|
}
|
||||||
return []int{}
|
return []int{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
```go
|
||||||
|
// 使用map方式解题,降低时间复杂度
|
||||||
|
func twoSum(nums []int, target int) []int {
|
||||||
|
m := make(map[int]int)
|
||||||
|
for index, val := range nums {
|
||||||
|
if preIndex, ok := m[target-val]; ok {
|
||||||
|
return []int{preIndex, index}
|
||||||
|
} else {
|
||||||
|
m[val] = index
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return []int{}
|
||||||
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
Rust
|
Rust
|
||||||
|
Reference in New Issue
Block a user