diff --git a/Two-Sum Problem/README.markdown b/Two-Sum Problem/README.markdown index a20666efd..6fb05d316 100644 --- a/Two-Sum Problem/README.markdown +++ b/Two-Sum Problem/README.markdown @@ -1,24 +1,24 @@ # Two-Sum Problem -You're given an array `a` with numbers. Write an algorithm that checks if there are any two entries in the array that add up to a given number `k`. In other words, is there any `a[i] + a[j] == k`? +You're given an array of numbers. Write an algorithm that checks if there are any two elements in the array that add up to a given number. There are a variety of solutions to this problem (some better than others). The following solutions both run in **O(n)** time. # Solution 1 -This solution uses a dictionary to store differences between each element in the array and the sum `k` that we're looking for. The dictionary also stores the indices of each element. +This solution uses a dictionary to store differences between each element in the array and the sum that we're looking for. The dictionary also stores the indices of each element. -With this approach, each key in the dictionary corresponds to a new target value. If one of the successive numbers from the array is equal to one of the dictionary's keys, then we know there exist two numbers that sum to `k`. +With this approach, each key in the dictionary corresponds to a new target value. If one of the successive numbers from the array is equal to one of the dictionary's keys, then we know there exist two numbers that sum. ```swift -func twoSumProblem(_ a: [Int], k: Int) -> ((Int, Int))? { - var dict = [Int: Int]() - - for i in 0 ..< a.count { - if let newK = dict[a[i]] { - return (newK, i) +func twoSumProblem(_ numbers: [Int], target: Int) -> (Int, Int)? { + var dict: [Int: Int] = [:] + + for (index, number) in numbers.enumerated() { + if let otherIndex = dict[number] { + return (index, otherIndex) } else { - dict[k - a[i]] = i + dict[target - number] = index } } diff --git a/Two-Sum Problem/Solution 1/2Sum.playground/Contents.swift b/Two-Sum Problem/Solution 1/2Sum.playground/Contents.swift index 4de46128c..4b6db5d51 100644 --- a/Two-Sum Problem/Solution 1/2Sum.playground/Contents.swift +++ b/Two-Sum Problem/Solution 1/2Sum.playground/Contents.swift @@ -4,26 +4,21 @@ print("Hello, Swift 4!") #endif -func twoSumProblem(_ a: [Int], k: Int) -> ((Int, Int))? { - var map = [Int: Int]() - - for i in 0 ..< a.count { - if let newK = map[a[i]] { - return (newK, i) +func twoSum(_ numbers: [Int], target: Int) -> (Int, Int)? { + var dict: [Int: Int] = [:] + + for (index, number) in numbers.enumerated() { + if let otherIndex = dict[number] { + return (index, otherIndex) } else { - map[k - a[i]] = i + dict[target - number] = index } } + return nil } -let a = [7, 100, 2, 21, 12, 10, 22, 14, 3, 4, 8, 4, 9] -if let (i, j) = twoSumProblem(a, k: 33) { - i // 3 - a[i] // 21 - j // 4 - a[j] // 12 - a[i] + a[j] // 33 -} +let numbers = [3, 2, 4] +let target = 6 -twoSumProblem(a, k: 37) // nil +twoSum(numbers, target: target) // expected output: indices 2 and 1