Skip to content

Commit

Permalink
0001.两数之和:简化Swift实现
Browse files Browse the repository at this point in the history
  • Loading branch information
bqlin committed Jan 8, 2022
1 parent 59b38a9 commit 3078fb8
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions problems/0001.两数之和.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,18 +207,16 @@ function twoSum(array $nums, int $target): array
Swift:
```swift
func twoSum(_ nums: [Int], _ target: Int) -> [Int] {
var res = [Int]()
var dict = [Int : Int]()
for i in 0 ..< nums.count {
let other = target - nums[i]
if dict.keys.contains(other) {
res.append(i)
res.append(dict[other]!)
return res
// 值: 下标
var map = [Int: Int]()
for (i, e) in nums.enumerated() {
if let v = map[target - e] {
return [v, i]
} else {
map[e] = i
}
dict[nums[i]] = i
}
return res
return []
}
```

Expand Down

0 comments on commit 3078fb8

Please sign in to comment.