[LeetCode] 167. Two Sum II - Input Array Is Sorted (Python)
- ์ด์ง ํ์ ๋ฐฉ๋ฒ๋ ์์ง๋ง ํฌํฌ์ธํฐ ๋ฐฉ์์ด ๋ ์ง๊ด์ ์ธ ๊ฒ ๊ฐ๋ค.
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 []
Leave a comment