diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 77318294..3d0674ce 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -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