-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0015-3sum.scala
36 lines (32 loc) · 1.16 KB
/
0015-3sum.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
import scala.collection.mutable.ListBuffer
object Solution {
def threeSum(nums: Array[Int]): List[List[Int]] = {
val res = ListBuffer[List[Int]]()
val sortedNums = nums.sorted
val endIdx = sortedNums.length - 1
for (i <- 0 to endIdx) {
val curr = sortedNums(i)
// To prevent duplicate triplet, but allow the triplet to contain same numbers
val shouldSkip = i > 0 && curr == sortedNums(i - 1)
if (!shouldSkip) {
var (s, e) = (i + 1, endIdx)
val target = 0 - curr
while (s < e) {
val sum = sortedNums(s) + sortedNums(e)
if (sum == target) {
res += List(curr, sortedNums(s), sortedNums(e))
e -= 1
while (e > 0 && sortedNums(e) == sortedNums(e + 1)) {
e -= 1
}
} else if (sum < target) {
s += 1
} else {
e -= 1
}
}
}
}
res.toList
}
}