LeetCode/Top Interview 150

167. Two Sum II - Input Array Is Sorted

hyunkookim 2024. 11. 29. 15:30

167. Two Sum II - Input Array Is Sorted

 

class Solution:
    def twoSum(self, numbers: List[int], target: int) -> List[int]:
        l, r = 0, len(numbers)-1
        res = []
        while l < r:
            if numbers[l]+numbers[r] < target:
                l +=1
            elif numbers[l]+numbers[r] > target:
                r -=1
            else: # numbers[l]+numbers[r] == target
                res.append(l+1)
                res.append(r+1)
                break
        return res

 

 

https://youtu.be/cQ1Oz4ckceM?si=cQZ68c1mfJdS072N

 

'LeetCode > Top Interview 150' 카테고리의 다른 글

209. Minimum Size Subarray Sum  (0) 2024.11.29
15. 3Sum  (0) 2024.11.29
125. Valid Palindrome  (0) 2024.11.28
68. Text Justification  (0) 2024.11.28
28. Find the Index of the First Occurrence in a String  (0) 2024.11.28