We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 861c770 commit 3b2373dCopy full SHA for 3b2373d
problems/0001.两数之和.md
@@ -287,17 +287,16 @@ func twoSum(nums []int, target int) []int {
287
```
288
289
```go
290
-// 使用map方式解题,降低时间复杂度
291
func twoSum(nums []int, target int) []int {
292
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
+ for i, num := range nums {
+ complement := target - num
+ if index, found := m[complement]; found {
+ return []int{index, i}
298
}
+ m[num] = i
299
300
- return []int{}
+ return nil // 返回空数组 nil 代替空切片
301
302
303
0 commit comments