添加 0001.两数之和 go版

使用map方式解题,降低时间复杂度
This commit is contained in:
NevS
2021-06-01 20:34:57 +08:00
committed by GitHub
parent 7b3f8ea4dd
commit 9219242b3a

View File

@ -134,6 +134,20 @@ func twoSum(nums []int, target int) []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