[Leetcode] 167. Two Sum II - Input Array Is Sorted

bradley·2022년 6월 9일
1

Algorithm

목록 보기
9/12

Problem

Solution

1) 투 포인터로 접근

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        left = 0
        right = len(numbers) - 1
        
        while left <= right:
            twoSum = numbers[left] + numbers[right]
            if twoSum == target:
                return [left + 1, right + 1]
            elif twoSum < target:
                left += 1
            elif twoSum > target:
                right -= 1
profile
데이터 엔지니어링에 관심이 많은 홀로 삽질하는 느림보

0개의 댓글