forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0015-3sum.swift
26 lines (26 loc) · 889 Bytes
/
0015-3sum.swift
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
class Solution {
func threeSum(_ nums: [Int]) -> [[Int]] {
let sortedArray = nums.sorted()
var result: [[Int]] = []
for i in 0..<sortedArray.count {
if i > 0 && sortedArray[i] == sortedArray[i - 1] { continue }
var l = i + 1
var r = sortedArray.count - 1
while l < r {
var threeSum = sortedArray[i] + sortedArray[l] + sortedArray[r]
if threeSum > 0 {
r -= 1
} else if threeSum < 0 {
l += 1
} else {
result.append([sortedArray[i], sortedArray[l], sortedArray[r]])
l += 1
while sortedArray[l] == sortedArray[l - 1] && l < r {
l += 1
}
}
}
}
return result
}
}