forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChaedie.py
More file actions
59 lines (48 loc) · 1.46 KB
/
Chaedie.py
File metadata and controls
59 lines (48 loc) · 1.46 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
"""
첫번째 풀이 -> 달레의 코드 풀이
1) sort와 two pointer를 활용한 풀이
2) has_set 을 활용한 중복 제거
두번째 풀이 -> Neetcode 풀이
1) sort와 two pointer를 활용한 풀이
2) while loop 를 활용한 중복 제거
Time: O(n^2) = O(n) * O(n)
Space: O(n)
"""
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
res = set()
n = len(nums)
for i in range(n):
l, r = i + 1, n - 1
while l < r:
summ = nums[i] + nums[l] + nums[r]
if summ < 0:
l += 1
elif summ > 0:
r -= 1
else:
res.add((nums[i], nums[l], nums[r]))
l += 1
return list(res)
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
nums.sort()
res = []
n = len(nums)
for i in range(n):
l, r = i + 1, n - 1
if i > 0 and nums[i] == nums[i - 1]:
continue
while l < r:
summ = nums[i] + nums[l] + nums[r]
if summ < 0:
l += 1
elif summ > 0:
r -= 1
else:
res.append([nums[i], nums[l], nums[r]])
l += 1
while nums[l] == nums[l - 1] and l < r:
l += 1
return res