상위 150개 인터뷰쉬움

투섬

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

문제 설명

쉬움

정수 배열 nums과 정수 target이 주어지면 두 숫자의 합이 target가 되도록 두 숫자의 인덱스를 반환합니다.

각 입력에는 정확히 하나의 솔루션이 있다고 가정할 수 있으며 동일한 요소를 두 번 사용할 수 없습니다.

어떤 순서로든 답변을 반환할 수 있습니다.

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

제약
  • 2 <= len(nums) <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Only one valid answer exists

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

nums[0] + nums[1] = 2 + 7 = 9, so we return [0, 1].

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

nums[1] + nums[2] = 2 + 4 = 6, so we return [1, 2].

Example 3
Input
nums = [3, 3], target = 6
Output
[0, 1]
Explanation

nums[0] + nums[1] = 3 + 3 = 6, so we return [0, 1].

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 리소스

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