Created
May 23, 2022 11:15
-
-
Save mdpabel/1ffda73ff71bec608e9a2b98be94f0f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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