parent
7b3f8ea4dd
commit
9219242b3a
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue