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 3b2373d

Browse files
authored
Update 0001.两数之和.md
1 parent 861c770 commit 3b2373d

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
@@ -287,17 +287,16 @@ func twoSum(nums []int, target int) []int {
287287
```
288288

289289
```go
290-
// 使用map方式解题,降低时间复杂度
291290
func twoSum(nums []int, target int) []int {
292291
m := make(map[int]int)
293-
for index, val := range nums {
294-
if preIndex, ok := m[target-val]; ok {
295-
return []int{preIndex, index}
296-
} else {
297-
m[val] = index
292+
for i, num := range nums {
293+
complement := target - num
294+
if index, found := m[complement]; found {
295+
return []int{index, i}
298296
}
297+
m[num] = i
299298
}
300-
return []int{}
299+
return nil// 返回空数组 nil 代替空切片
301300
}
302301
```
303302

0 commit comments

Comments
(0)

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