forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelight010.swift
More file actions
25 lines (23 loc) · 785 Bytes
/
delight010.swift
File metadata and controls
25 lines (23 loc) · 785 Bytes
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
class Solution {
func threeSum(_ nums: [Int]) -> [[Int]] {
let nums = nums.sorted()
var answer: Set<[Int]> = []
for (index, num) in nums.enumerated() {
var leftIndex = index + 1
var rightIndex = nums.endIndex - 1
while leftIndex < rightIndex {
let sum = num + nums[leftIndex] + nums[rightIndex]
if sum == 0 {
answer.insert([num, nums[leftIndex], nums[rightIndex]])
leftIndex += 1
rightIndex -= 1
} else if sum < 0 {
leftIndex += 1
} else if sum > 0 {
rightIndex -= 1
}
}
}
return Array(answer)
}
}