상위 150개 인터뷰쉬움

투섬 II

'Two Sum II' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

이미 감소하지 않는 순서로 정렬된 1부터 인덱스가 지정된 정수 배열 numbers이 주어지면 특정 target 숫자에 더해지는 두 개의 숫자를 찾습니다.

두 숫자 index1index2의 인덱스를 길이 2의 정수 배열 [index1, index2]로 1씩 더한 값으로 반환합니다.

동일한 요소를 두 번 사용할 수 없습니다. 솔루션은 일정한 추가 공간만 사용해야 합니다.

twoSum(numbers: List[int], target: int) -> List[int] 함수를 작성하세요.

제약
  • 2 <= len(numbers) <= 3 * 10^4
  • -1000 <= numbers[i] <= 1000
  • numbers is sorted in non-decreasing order
  • -1000 <= target <= 1000
  • Exactly one solution exists

Example 1
Input
numbers = [2, 7, 11, 15], target = 9
Output
[1, 2]
Explanation

2 + 7 = 9. The indices are 1 and 2 (1-indexed).

Example 2
Input
numbers = [2, 3, 4], target = 6
Output
[1, 3]
Explanation

2 + 4 = 6. The indices are 1 and 3 (1-indexed).

Example 3
Input
numbers = [-1, 0], target = -1
Output
[1, 2]
Explanation

-1 + 0 = -1. The indices are 1 and 2 (1-indexed).

Need a Hint?
세트나 힙과 같은 두 포인터 관련 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.