Skip to content

Commit 48d83f2

Browse files
authored
Updates readme based on changes to playground code.
1 parent 26052e7 commit 48d83f2

File tree

1 file changed

+18
-23
lines changed

1 file changed

+18
-23
lines changed

3Sum and 4Sum/README.md

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ extension Collection where Element: Equatable {
3131
3232
/// Returns next index with unique value. Works only on sorted arrays.
3333
///
34-
/// - Parameter index: The current `Int` index.
35-
/// - Returns: The new `Int` index. Will return `nil` if new index happens to be the `endIndex` (out of bounds)
34+
/// - Parameter index: The current index.
35+
/// - Returns: The new index. Will return `nil` if new index happens to be the `endIndex` (out of bounds)
3636
func uniqueIndex(after index: Index) -> Index? {
3737
guard index < endIndex else { return nil }
3838
var index = index
@@ -41,12 +41,7 @@ extension Collection where Element: Equatable {
4141
formIndex(after: &index)
4242
formIndex(after: &nextIndex)
4343
}
44-
45-
if nextIndex == endIndex {
46-
return nil
47-
} else {
48-
return nextIndex
49-
}
44+
return nextIndex != endIndex ? nextIndex : nil
5045
}
5146
}
5247
```
@@ -58,8 +53,8 @@ extension BidirectionalCollection where Element: Equatable {
5853
5954
/// Returns next index with unique value. Works only on sorted arrays.
6055
///
61-
/// - Parameter index: The current `Int` index.
62-
/// - Returns: The new `Int` index. Will return `nil` if new index happens to come before the `startIndex` (out of bounds)
56+
/// - Parameter index: The current index.
57+
/// - Returns: The new index. Will return `nil` if new index happens to come before the `startIndex` (out of bounds)
6358
func uniqueIndex(before index: Index) -> Index? {
6459
return indices[..<index].reversed().first { index -> Bool in
6560
let nextIndex = self.index(after: index)
@@ -83,29 +78,29 @@ You'll keep track of 3 indices to represent the 3 numbers. The sum at any given
8378
The premise is quite straightforward (given that you're familiar with 2Sum). You'll iterate `l` through the array. For every iteration, you also apply the 2Sum algorithm to elements after `l`. You'll check the sum every time you moving the indices to check if you found match. Here's the algorithm:
8479

8580
```
86-
func threeSum<T: BidirectionalCollection>(_ collection: T, target: T.Element) -> [[T.Element]] where T.Element: BinaryInteger & Comparable {
87-
let sorted = collection.sorted()
81+
func threeSum<T: BidirectionalCollection>(_ c: T, target: T.Element) -> [[T.Element]] where T.Element: Numeric & Comparable {
82+
let sorted = c.sorted()
8883
var ret: [[T.Element]] = []
8984
9085
for l in sequence(first: sorted.startIndex, next: sorted.uniqueIndex(after:)) {
9186
var m = sorted.index(after: l)
9287
var r = sorted.index(before: sorted.endIndex)
93-
88+
9489
while m < r {
9590
let sum = sorted[l] + sorted[m] + sorted[r]
96-
switch target {
97-
case sum:
91+
switch sum {
92+
case target:
9893
ret.append([sorted[l], sorted[m], sorted[r]])
99-
guard let nextMid = sorted.uniqueIndex(after: m), let nextRight = sorted.uniqueIndex(before: r) else { break }
100-
m = nextMid
101-
r = nextRight
94+
guard let nextM = sorted.uniqueIndex(after: m), let nextR = sorted.uniqueIndex(before: r) else { break }
95+
m = nextM
96+
r = nextR
10297
case ..<target:
103-
guard let nextMid = sorted.uniqueIndex(after: m) else { break }
104-
m = nextMid
98+
guard let nextM = sorted.uniqueIndex(after: m) else { break }
99+
m = nextM
105100
case target...:
106-
guard let nextRight = sorted.uniqueIndex(before: r) else { break }
107-
r = nextRight
108-
default: fatalError("Swift isn't smart enough to detect that this switch statement is exhausive")
101+
guard let nextR = sorted.uniqueIndex(before: m) else { break }
102+
r = nextR
103+
default: fatalError()
109104
}
110105
}
111106
}

0 commit comments

Comments
 (0)