[LeetCode/Python] 167. Two Sum II - Input Array Is Sorted

도니·2025년 9월 28일

Interview-Prep

목록 보기
21/29

📌 Problem

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

📌 Solution

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        n = len(numbers)
        l, r = 0, n-1
        
        while l < r:
            res = numbers[l] + numbers[r]
            if res == target:
                return [l+1, r+1]
            elif res > target:
                r -= 1
            else: # res < target:
                l += 1
        
        return [l, r]
profile
Where there's a will, there's a way

0개의 댓글