Skip to content

Commit 1c06763

Browse files
author
Chris Pilcher
authored
Merge pull request kodecocodes#266 from JaapWijnen/two-sum-migrate-swift3
Two sum migrate swift3
2 parents e340318 + 0227ee1 commit 1c06763

File tree

6 files changed

+9
-58
lines changed

6 files changed

+9
-58
lines changed

Two-Sum Problem/README.markdown

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ There are a variety of solutions to this problem (some better than others). The
88

99
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.
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 to `k`.
1212

1313
```swift
14-
func twoSumProblem(a: [Int], k: Int) -> ((Int, Int))? {
14+
func twoSumProblem(_ a: [Int], k: Int) -> ((Int, Int))? {
1515
var dict = [Int: Int]()
1616

1717
for i in 0 ..< a.count {
@@ -69,7 +69,7 @@ The running time of this algorithm is **O(n)** because it potentially may need t
6969
Here is the code in Swift:
7070

7171
```swift
72-
func twoSumProblem(a: [Int], k: Int) -> ((Int, Int))? {
72+
func twoSumProblem(_ a: [Int], k: Int) -> ((Int, Int))? {
7373
var i = 0
7474
var j = a.count - 1
7575

@@ -78,9 +78,9 @@ func twoSumProblem(a: [Int], k: Int) -> ((Int, Int))? {
7878
if sum == k {
7979
return (i, j)
8080
} else if sum < k {
81-
++i
81+
i += 1
8282
} else {
83-
--j
83+
j -= 1
8484
}
8585
}
8686
return nil

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//: Playground - noun: a place where people can play
22

3-
func twoSumProblem(a: [Int], k: Int) -> ((Int, Int))? {
3+
func twoSumProblem(_ a: [Int], k: Int) -> ((Int, Int))? {
44
var map = [Int: Int]()
55

66
for i in 0 ..< a.count {

Two-Sum Problem/Solution 1/2Sum.playground/timeline.xctimeline

-6
This file was deleted.

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

-22
This file was deleted.

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//: Playground - noun: a place where people can play
22

3-
func twoSumProblem(a: [Int], k: Int) -> ((Int, Int))? {
3+
func twoSumProblem(_ a: [Int], k: Int) -> ((Int, Int))? {
44
var i = 0
55
var j = a.count - 1
66

@@ -9,9 +9,9 @@ func twoSumProblem(a: [Int], k: Int) -> ((Int, Int))? {
99
if sum == k {
1010
return (i, j)
1111
} else if sum < k {
12-
++i
12+
i += 1
1313
} else {
14-
--j
14+
j -= 1
1515
}
1616
}
1717
return nil

Two-Sum Problem/Solution 2/2Sum.swift

-21
This file was deleted.

0 commit comments

Comments
 (0)