[LeetCode][Python] 167. Two Sum II - Input Array Is Sorted
Online Judge/LeetCode | 2024. 9. 8. 01:26
- 문제 링크: https://leetcode.com/problems/two-sum-ii-input-array-is-sorted
- 난이도: Medium
정렬된 배열에서 합쳤을 때 특정 값이 되는 원소 두개의 인덱스를 찾는 문제다
투포인터를 사용한다
class Solution:
def twoSum(self, numbers: List[int], target: int) -> List[int]:
left, right = 0, len(numbers)-1
while left < right:
curr = numbers[left] + numbers[right]
if curr == target:
return [left + 1, right + 1]
elif curr < target:
left += 1
else:
right -= 1
반응형
'Online Judge > LeetCode' 카테고리의 다른 글
[LeetCode][Python] 11. Container With Most Water (0) | 2024.09.08 |
---|---|
[LeetCode][Python] 15. 3Sum (0) | 2024.09.08 |
[LeetCode][Python] 125. Valid Palindrome (0) | 2024.09.08 |
[LeetCode][Python] 128. Longest Consecutive Sequence (0) | 2024.09.08 |
[LeetCode][Python] 36. Valid Sudoku (0) | 2024.09.08 |