리트코드 167번
정렬된 리스트에서 두 수의 합이 target값이 되는 인덱스 반환하기
투포인터
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
i = 0
j = len(numbers)-1
while i < j:
if numbers[i] + numbers[j] > target:
j = j - 1
elif numbers[i] + numbers[j] < target:
i = i + 1
else:
return [i+1,j+1]
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
for k, v in enumerate(numbers):
left, right = k+1, len(numbers)-1
expected = target - v
while left <= right:
mid = left + (right-left)//2
if numbers[mid] < expected :
left = mid + 1
elif numbers[mid] > expected:
right = mid -1
else:
return k+1, mid+1