Skip to content

Commit 472e827

Browse files
committed
add two sum annotations
1 parent a9db90e commit 472e827

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33

44
class Solution:
5-
def twoSum(self, nums: List[int], target: int) -> List[int]:
5+
"""
6+
Ideation:
7+
target = x + y -> target - x = y
8+
x ์›์†Œ๋ฅผ ๊ฐ€์žฅ ๋ฐ”๊นฅ์˜ ์ดํ„ฐ๋ ˆ์ด์…˜์—์„œ ๋Œ๋ฉด์„œ ํ•˜๋‚˜์”ฉ ๋Œ€์ž…ํ•ฉ๋‹ˆ๋‹ค.
9+
์ฐพ๊ณ ์ž ํ•˜๋Š” ๊ฐ’์ด (x,y) ์Œ์ด๋ฏ€๋กœ, ๋‘๊ฐœ์˜ ์ธ๋ฑ์Šค๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด ์ธ๋ฑ์Šค ๊ธฐ์ค€์œผ๋กœ ์ดํ„ฐ๋ ˆ์ด์…˜์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค( enumerate ์‚ฌ์šฉ ๊ฐ€๋Šฅ).
10+
y๊ฐ’์ด ์ฐพ์•„์ง€๋ฉด ๋‹น์‹œ์˜ x๊ฐ’์˜ ์ธ๋ฑ์Šค์™€ ํ•จ๊ป˜ (index_of_x, index_of_y) ๋ฅผ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
11+
Time Complexity: O(N^2)
12+
Space Complexity: O(1)
13+
"""
614

15+
def twoSum(self, nums: List[int], target: int) -> List[int]:
716
for i in range(0, len(nums) - 1):
817
for j in range(i + 1, len(nums)):
918
if (target - nums[i]) == nums[j]:

0 commit comments

Comments
ย (0)