Skip to content

Commit da9e823

Browse files
authored
Simplified switch statement to if-else loop
1 parent 676fbbe commit da9e823

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

3Sum and 4Sum/3Sum.playground/Contents.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,14 @@ func threeSum<T: BidirectionalCollection>(_ collection: T, target: T.Element) ->
3838

3939
while m < r && r < sorted.endIndex {
4040
let sum = sorted[l] + sorted[m] + sorted[r]
41-
switch target {
42-
case sum:
41+
if sum == target {
4342
ret.append([sorted[l], sorted[m], sorted[r]])
4443
sorted.formUniqueIndex(after: &m)
4544
sorted.formUniqueIndex(before: &r)
46-
case ..<target:
45+
} else if sum < target {
4746
sorted.formUniqueIndex(after: &m)
48-
case target...:
47+
} else {
4948
sorted.formUniqueIndex(before: &r)
50-
default: fatalError("Swift isn't smart enough to detect that this switch statement is exhausive")
5149
}
5250
}
5351
}

0 commit comments

Comments
 (0)