mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 00:43:04 +08:00
添加 1. 两数之和 Swift版本
This commit is contained in:
@ -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)
|
||||||
|
Reference in New Issue
Block a user