diff --git a/leetcode/1 - twosum/two_sum.swift b/leetcode/1 - twosum/two_sum.swift new file mode 100644 index 0000000..2ce8c85 --- /dev/null +++ b/leetcode/1 - twosum/two_sum.swift @@ -0,0 +1,19 @@ +import Cocoa + +class Solution { + func twoSum(_ nums: [Int], _ target: Int) -> [Int] { + var seen: Set = [] + + for i in 0 ... nums.count - 1 { + let complement = target - nums[i] + if seen.contains(complement) { + if let complementIndex = nums.firstIndex(of: complement) { + return([complementIndex,i]) + } + } else { + seen.insert(nums[i]) + } + } + return [] + } +}