Published:
Updated:

  • ์ด์ง„ ํƒ์ƒ‰ ๋ฐฉ๋ฒ•๋„ ์žˆ์ง€๋งŒ ํˆฌํฌ์ธํ„ฐ ๋ฐฉ์‹์ด ๋” ์ง๊ด€์ ์ธ ๊ฒƒ ๊ฐ™๋‹ค.


SolutionPermalink

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 []


ReferencePermalink

Leave a comment