Skip to content

Instantly share code, notes, and snippets.

@mdpabel
Created May 23, 2022 11:15
Show Gist options
  • Select an option

  • Save mdpabel/1ffda73ff71bec608e9a2b98be94f0f1 to your computer and use it in GitHub Desktop.

Select an option

Save mdpabel/1ffda73ff71bec608e9a2b98be94f0f1 to your computer and use it in GitHub Desktop.
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
sorted_nums = sorted(nums)
res = []
left = 0;
right = len(sorted_nums) - 1
while right >= left:
current_sum = sorted_nums[left] + sorted_nums[right]
if current_sum == target:
break
elif current_sum > target:
right -= 1
else:
left += 1
for i in range(len(nums)):
if nums[i] == sorted_nums[left]:
res.append(i)
break
for j in reversed(range(len(nums))):
print(j)
if nums[j] == sorted_nums[right]:
res.append(j)
break
return res
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
seen = {}
for i in range(len(nums)):
expected_num = target - nums[i]
if expected_num in seen:
return [seen[expected_num], i]
else:
seen[nums[i]] = i
return []
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment