forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheypaprika.py
More file actions
30 lines (26 loc) ยท 1.02 KB
/
heypaprika.py
File metadata and controls
30 lines (26 loc) ยท 1.02 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
# 2Sum์ ํ์ฉํ ํ์ด
# Note : ์ ๋ ฌ ํ ํธ๋ ๊ฒ์ด ๋ ์ง๊ด์ ์ด๊ณ , ๋น ๋ฆ
"""
๋ณต์ก๋ : ์์ -> ์์ํ ์ด์
์๊ฐ ๋ณต์ก๋ : O(n^2) -> nums ๋ฐฐ์ด 2์ค for๋ฌธ
๊ณต๊ฐ ๋ณต์ก๋ : O(n) -> ์ต์
์ ๊ฒฝ์ฐ nums ๋ฐฐ์ด ๊ธธ์ด๋งํผ์ ๋์
๋๋ฆฌ ์์ฑ
"""
class Solution:
def threeSum(self, nums: List[int]) -> List[List[int]]:
ans_list = set()
added_pair = {}
for target_i, target_number in enumerate(nums):
cur_ans_list = []
num_dict = {}
if target_number in added_pair:
continue
for i in range(target_i + 1, len(nums)):
num_A = nums[i]
num_B = - target_number - num_A
if num_B in num_dict:
cur_ans_list.append(sorted([target_number, num_A, num_B]))
added_pair[target_number] = num_A
num_dict[num_A] = 1
for item in cur_ans_list:
ans_list.add(tuple(item))
return list(ans_list)