Skip to content

Commit 25e9a07

Browse files
committed
fix: two-sum
ํ”ผ๋“œ๋ฐฑ ๋ฐ›์€ ์ฝ”๋“œ ๊ธฐ์กด ํŒŒ์ผ์— ์ฃผ์„์œผ๋กœ ์ถ”๊ฐ€
1 parent 74858ea commit 25e9a07

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

โ€Žtwo-sum/ZetBe.pyโ€Ž

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,18 @@
1919
์ตœ์•…์˜ ๊ฒฝ์šฐ, n*(n-1)/2๋ฒˆ์˜ ํƒ์ƒ‰์ด ํ•„์š”ํ•œ๋ฐ, ์ด๋Š” ๊ฒฐ๊ตญ O(n^2)์ด๋‹ค.
2020
'''
2121

22+
# ์ถ”๊ฐ€๋กœ ์ฝ”๋“œ๋ฆฌ๋ทฐ๋ฅผ ํ†ตํ•ด ๊ฐ€๋…์„ฑ ๊ฐœ์„ ํ•œ ์ฝ”๋“œ
23+
'''
24+
for i in range(len(nums)):
25+
if target - nums[i] in d:
26+
return [d[target-nums[i]][0], i]
27+
if nums[i] not in d:
28+
d[nums[i]] = [i]
29+
30+
else:
31+
32+
d[nums[i]] += [i]
33+
'''
2234

2335
class Solution:
2436
def twoSum(self, nums: List[int], target: int) -> List[int]:
@@ -37,3 +49,4 @@ def twoSum(self, nums: List[int], target: int) -> List[int]:
3749
if target-i in d and len(d[target-i]) >= 2 and target == i*2:
3850
answ = [d[target-i][0], d[i][1]]
3951
return answ
52+

0 commit comments

Comments
ย (0)