Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c1f4e6f

Browse files
Merge pull request #2882 from buptz2b/master
Update 0001.两数之和.md
2 parents 2b0ca46 + 3b2373d commit c1f4e6f

File tree

1 file changed

+6
-7
lines changed

1 file changed

+6
-7
lines changed

‎problems/0001.两数之和.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -285,17 +285,16 @@ func twoSum(nums []int, target int) []int {
285285
```
286286

287287
```go
288-
// 使用map方式解题,降低时间复杂度
289288
func twoSum(nums []int, target int) []int {
290289
m := make(map[int]int)
291-
for index, val := range nums {
292-
if preIndex, ok := m[target-val]; ok {
293-
return []int{preIndex, index}
294-
} else {
295-
m[val] = index
290+
for i, num := range nums {
291+
complement := target - num
292+
if index, found := m[complement]; found {
293+
return []int{index, i}
296294
}
295+
m[num] = i
297296
}
298-
return []int{}
297+
return nil// 返回空数组 nil 代替空切片
299298
}
300299
```
301300

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /