forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhu6r1s.py
More file actions
28 lines (25 loc) ยท 923 Bytes
/
hu6r1s.py
File metadata and controls
28 lines (25 loc) ยท 923 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:
"""
1. 3 way for๋ฌธ์ผ๋ก ๋์๊ฐ๋ฉด์ 0์ธ ํฉ์ ์ฐพ๋ ๋ฐฉ๋ฒ
- O(n^3)์ผ๋ก ์๊ฐ์ด๊ณผ
2. ํฌํฌ์ธํฐ
- ์ ๋ ฌ ํ ํฌํฌ์ธํฐ๋ฅผ ์ด์ฉํ์ฌ ์ค๋ณต ์ ๊ฑฐ์ ์ต์ ํ๋ฅผ ๋์์ ์ํ
- O(n^2)
๊ณต๊ฐ ๋ณต์ก๋๋ ๋๋ค O(1)
"""
def threeSum(self, nums: List[int]) -> List[List[int]]:
result = set()
nums.sort()
for i in range(len(nums)):
left, right = i + 1, len(nums) - 1
while left < right:
total = nums[i] + nums[left] + nums[right]
if total == 0:
result.add((nums[i], nums[left], nums[right]))
left += 1
right -= 1
elif total < 0:
left += 1
else:
right -= 1
return list(result)