Skip to content

Commit 593590d

Browse files
committed
neetcode two sum
1 parent 3bbbd04 commit 593590d

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
def twoSum(self, nums: List[int], target: int) -> List[int]:
6+
numbers = sorted(nums)
7+
left = 0
8+
right = len(numbers) - 1
9+
10+
while left < right:
11+
sum = numbers[left] + numbers[right]
12+
if sum == target:
13+
if numbers[left] == numbers[right]:
14+
left_index = nums.index(numbers[left])
15+
right_index = nums.index(numbers[right], left_index + 1)
16+
return [left_index, right_index]
17+
else:
18+
return [nums.index(numbers[left]), nums.index(numbers[right])]
19+
20+
if sum < target:
21+
left += 1
22+
elif sum > target:
23+
right -= 1

0 commit comments

Comments
 (0)