[
2sum
two pointers
]
BinarySearch 0800 Sum of Two Numbers with Sorted List
Problem statement
https://binarysearch.com/problems/Sum-of-Two-Numbers-with-Sorted-List/
Solution
Equal to Leetcode 0167. Two Sum II - Input array is sorted.
Complexity
It is O(n)
for time and O(1)
for space.
Code
class Solution:
def solve(self, nums, target):
beg, end = 0, len(nums) - 1
while beg < end:
cand = nums[beg] + nums[end]
if cand > target:
end -= 1
elif cand < target:
beg += 1
else:
return True
return False