添加 1. 两数之和 Swift版本

This commit is contained in:
极客学伟
2021-09-01 14:45:25 +08:00
parent c2d5b48605
commit 9d1983fd02

View File

@ -206,6 +206,23 @@ 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
}
dict[nums[i]] = i
}
return res
}
```
----------------------- -----------------------
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw) * 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)