Skip to content

Commit f0544d0

Browse files
authored
Merge pull request kodecocodes#620 from raywenderlich/2-sum-updates
Updated solution 1 for clarity.
2 parents 45df8d4 + d757cbe commit f0544d0

File tree

2 files changed

+21
-26
lines changed

2 files changed

+21
-26
lines changed

Two-Sum Problem/README.markdown

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
# Two-Sum Problem
22

3-
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`?
3+
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.
44

55
There are a variety of solutions to this problem (some better than others). The following solutions both run in **O(n)** time.
66

77
# Solution 1
88

9-
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.
9+
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.
1010

11-
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`.
11+
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.
1212

1313
```swift
14-
func twoSumProblem(_ a: [Int], k: Int) -> ((Int, Int))? {
15-
var dict = [Int: Int]()
16-
17-
for i in 0 ..< a.count {
18-
if let newK = dict[a[i]] {
19-
return (newK, i)
14+
func twoSumProblem(_ numbers: [Int], target: Int) -> (Int, Int)? {
15+
var dict: [Int: Int] = [:]
16+
17+
for (index, number) in numbers.enumerated() {
18+
if let otherIndex = dict[number] {
19+
return (index, otherIndex)
2020
} else {
21-
dict[k - a[i]] = i
21+
dict[target - number] = index
2222
}
2323
}
2424

Two-Sum Problem/Solution 1/2Sum.playground/Contents.swift

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,26 +4,21 @@
44
print("Hello, Swift 4!")
55
#endif
66

7-
func twoSumProblem(_ a: [Int], k: Int) -> ((Int, Int))? {
8-
var map = [Int: Int]()
9-
10-
for i in 0 ..< a.count {
11-
if let newK = map[a[i]] {
12-
return (newK, i)
7+
func twoSum(_ numbers: [Int], target: Int) -> (Int, Int)? {
8+
var dict: [Int: Int] = [:]
9+
10+
for (index, number) in numbers.enumerated() {
11+
if let otherIndex = dict[number] {
12+
return (index, otherIndex)
1313
} else {
14-
map[k - a[i]] = i
14+
dict[target - number] = index
1515
}
1616
}
17+
1718
return nil
1819
}
1920

20-
let a = [7, 100, 2, 21, 12, 10, 22, 14, 3, 4, 8, 4, 9]
21-
if let (i, j) = twoSumProblem(a, k: 33) {
22-
i // 3
23-
a[i] // 21
24-
j // 4
25-
a[j] // 12
26-
a[i] + a[j] // 33
27-
}
21+
let numbers = [3, 2, 4]
22+
let target = 6
2823

29-
twoSumProblem(a, k: 37) // nil
24+
twoSum(numbers, target: target) // expected output: indices 2 and 1

0 commit comments

Comments
 (0)