diff --git "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" index e982ae129b..3c92e4dfeb 100644 --- "a/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" +++ "b/problems/0001.\344\270\244\346\225\260\344\271\213\345\222\214.md" @@ -287,17 +287,16 @@ func twoSum(nums []int, target int) []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 + for i, num := range nums { + complement := target - num + if index, found := m[complement]; found { + return []int{index, i} } + m[num] = i } - return []int{} + return nil // 返回空数组 nil 代替空切片 } ```