forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJustHm.swift
More file actions
28 lines (27 loc) · 951 Bytes
/
JustHm.swift
File metadata and controls
28 lines (27 loc) · 951 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
26
27
28
class Solution {
// time: O(n2) space: O(n)..?
func threeSum(_ nums: [Int]) -> [[Int]] {
let nums = nums.sorted() // hashmap 방식으로는 안될거 같아 정렬후 순차탐색 하기
var answer = Set<[Int]>() // 중복 제거를 위해 Set으로 선언
for i in nums.indices {
var left = i + 1
var right = nums.count - 1
while left < right {
let result = nums[left] + nums[right] + nums[i]
if result == 0 {
answer.insert([nums[i], nums[left], nums[right]])
// 포인터 옮겨주고 더 검사하기
right -= 1
left += 1
}
else if result > 0 {
right -= 1
}
else {
left += 1
}
}
}
return Array(answer)
}
}