Merge pull request #2882 from buptz2b/master

Update 0001.两数之和.md
This commit is contained in:
程序员Carl
2025-06-15 20:33:02 +08:00
committed by GitHub

View File

@ -285,17 +285,16 @@ func twoSum(nums []int, target int) []int {
``` ```
```go ```go
// 使用map方式解题降低时间复杂度
func twoSum(nums []int, target int) []int { func twoSum(nums []int, target int) []int {
m := make(map[int]int) m := make(map[int]int)
for index, val := range nums { for i, num := range nums {
if preIndex, ok := m[target-val]; ok { complement := target - num
return []int{preIndex, index} if index, found := m[complement]; found {
} else { return []int{index, i}
m[val] = index
} }
m[num] = i
} }
return []int{} return nil // 返回空数组 nil 代替空切片
} }
``` ```