diff --git a/swift/1-Two-Sum.swift b/swift/1-Two-Sum.swift new file mode 100644 index 000000000..0f4684a99 --- /dev/null +++ b/swift/1-Two-Sum.swift @@ -0,0 +1,14 @@ +class Solution { + func twoSum(_ nums: [Int], _ target: Int) -> [Int] { + var prevMap = [Int:Int]() // val -> index + + for (i, n) in nums.enumerated() { + let diff = target - n + if let firstIndex = prevMap[diff] { + return [firstIndex, i] + } + prevMap[n] = i + } + return [] + } +}