You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 3Sum and 4Sum/README.md
+18-23Lines changed: 18 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -31,8 +31,8 @@ extension Collection where Element: Equatable {
31
31
32
32
/// Returns next index with unique value. Works only on sorted arrays.
33
33
///
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)
36
36
func uniqueIndex(after index: Index) -> Index? {
37
37
guard index < endIndex else { return nil }
38
38
var index = index
@@ -41,12 +41,7 @@ extension Collection where Element: Equatable {
41
41
formIndex(after: &index)
42
42
formIndex(after: &nextIndex)
43
43
}
44
-
45
-
if nextIndex == endIndex {
46
-
return nil
47
-
} else {
48
-
return nextIndex
49
-
}
44
+
return nextIndex != endIndex ? nextIndex : nil
50
45
}
51
46
}
52
47
```
@@ -58,8 +53,8 @@ extension BidirectionalCollection where Element: Equatable {
58
53
59
54
/// Returns next index with unique value. Works only on sorted arrays.
60
55
///
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)
63
58
func uniqueIndex(before index: Index) -> Index? {
64
59
return indices[..<index].reversed().first { index -> Bool in
65
60
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
83
78
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:
0 commit comments