상위 150개 인터뷰쉬움

3섬

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

문제 설명

쉬움

정수 배열 nums이 주어지면 i != j, i != k, j != knums[i] + nums[j] + nums[k] == 0와 같은 세 쌍의 [nums[i], nums[j], nums[k]]을 모두 반환합니다.

솔루션 세트에는 중복된 삼중항이 포함되어서는 안 됩니다.

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

제약
  • 3 <= len(nums) <= 3000
  • -10^5 <= nums[i] <= 10^5

Example 1
Input
nums = [-1, 0, 1, 2, -1, -4]
Output
[[-1, -1, 2], [-1, 0, 1]]
Explanation

nums[0] + nums[1] + nums[2] = -1 + 0 + 1 = 0. nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0. nums[0] + nums[3] + nums[4] = -1 + 2 + (-1) = 0. The distinct triplets are [-1,-1,2] and [-1,0,1].

Example 2
Input
nums = [0, 1, 1]
Output
[]
Explanation

No triplet sums to 0.

Example 3
Input
nums = [0, 0, 0]
Output
[[0, 0, 0]]
Explanation

The only possible triplet sums to 0.

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

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