-
-
Notifications
You must be signed in to change notification settings - Fork 335
Expand file tree
/
Copy pathppxyn1.py
More file actions
34 lines (28 loc) · 1.16 KB
/
ppxyn1.py
File metadata and controls
34 lines (28 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# idea: For each number n in nums, check if (target - n) exists in the remaining elements.
class Solution:
def twoSum(self, nums: List[int], target: int) -> List[int]:
for idx, num in enumerate(nums):
required_num = target - num
if required_num in nums[idx+1:]:
return [idx, nums.index(required_num, idx+1)]
'''
Trial and error
idea : two pointer
I struggled to handle the indices of the original array after sorting it.
The code below fails when negative numbers are involved.
I realized it would be tricky to solve this problem with the two-pointer.
'''
# class Solution:
# def twoSum(self, nums: List[int], target: int) -> List[int]:
# sorted_nums = sorted(nums)
# left, right = 0, len(nums) - 1
# while left < right:
# s = sorted_nums[left] + sorted_nums[right]
# if s == target:
# left_idx = nums.index(sorted_nums[left])
# right_idx = nums.index(sorted_nums[right], left_idx + 1)
# return [left_idx, right_idx]
# elif s < target:
# left += 1
# else:
# right -= 1