diff --git a/problems/0001.两数之和.md b/problems/0001.两数之和.md index 37c95736..8551274c 100644 --- a/problems/0001.两数之和.md +++ b/problems/0001.两数之和.md @@ -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 [] } ```