forked from keon/algorithms
-
Notifications
You must be signed in to change notification settings - Fork 12
/
ThreeSum.scala
54 lines (47 loc) · 1.37 KB
/
ThreeSum.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package algorithms.array
/**
* Given an array S of n integers, are there elements a, b, c in S
* such that a + b + c = 0?
* Find all unique triplets in the array which gives the sum of zero.
*
* Note: The solution set must not contain duplicate triplets.
*
* For example, given array S = [-1, 0, 1, 2, -1, -4],
*
* A solution set is:
* [
* [-1, 0, 1],
* [-1, -1, 2]
* ]
*/
object ThreeSum {
def apply(list: List[Int]): Set[List[Int]] = {
if (list.size < 3) throw new IllegalArgumentException(s"Size of input list is less than 3")
val source = list.sorted
var solutionSet: Set[List[Int]] = Set.empty
for (i <- 0 until list.size - 2) {
if (!(i > 0 && source.apply(i) == source.apply(i - 1))) {
var (l: Int, r: Int) = (i + 1, list.size - 1)
while (l < r) {
val s = source.apply(i) + source.apply(l) + source.apply(r)
if (s > 0) {
r -= 1
} else if (s < 0) {
l += 1
} else {
solutionSet += List(source.apply(i), source.apply(l), source.apply(r))
while (l < r && source.apply(l) == source.apply(l + 1)) {
l += 1
}
while (l < r && source.apply(r) == source.apply(r - 1)) {
r -= 1
}
l += 1
r -= 1
}
}
}
}
solutionSet
}
}