[LeetCode] 167. Two Sum II - Input Array Is Sorted (Python)
- μ΄μ§ νμ λ°©λ²λ μμ§λ§ ν¬ν¬μΈν° λ°©μμ΄ λ μ§κ΄μ μΈ κ² κ°λ€.
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 []
Leave a comment