Published:
Updated:

  • 이진 탐색 방법도 μžˆμ§€λ§Œ νˆ¬ν¬μΈν„° 방식이 더 직관적인 것 κ°™λ‹€.


Solution

from typing import List


class Solution:
    # @return 2개의 μ •λ‹΅ indexμ—μ„œ +1을 ν•œ 두 값을 μ €μž₯ν•˜λŠ” λ°°μ—΄
    def twoSum(self, numbers: List[int], target: int) -> List[int]:

        lt, rt = 0, len(numbers) - 1
        while lt <= rt:
            sum_ = numbers[lt] + numbers[rt]

            if sum_ == target:
                return [lt + 1, rt + 1]

            if sum_ < target:
                lt += 1
                continue

            if sum_ > target:
                rt -= 1

        return []


Reference

Leave a comment