상위 150개 인터뷰중간

그래프 복제

'복제 그래프' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

중간

연결된 무방향 그래프의 노드 참조가 주어지면 그래프의 전체 복사본(클론)을 반환합니다.

그래프의 각 노드에는 값(int)과 이웃 목록(List[Node])이 포함되어 있습니다.

그래프는 인접 목록으로 표현됩니다. adjList[i]가 노드 i+1(1-인덱스)의 이웃을 나타내는 cloneGraph(adjList: List[List[int]]) -> List[List[int]] 함수를 구현하세요. 복제된 그래프의 인접 목록을 반환합니다.

제약
  • The number of nodes in the graph is in the range [0, 100]
  • 1 <= Node.val <= 100
  • Node.val is unique for each node
  • There are no repeated edges and no self-loops in the graph

Example 1
Input
adjList = [[2,4],[1,3],[2,4],[1,3]]
Output
[[2,4],[1,3],[2,4],[1,3]]
Explanation

Node 1's neighbors are 2 and 4. Node 2's neighbors are 1 and 3. Node 3's neighbors are 2 and 4. Node 4's neighbors are 1 and 3.

Example 2
Input
adjList = [[]]
Output
[[]]
Explanation

The graph contains only one node with value 1 and no neighbors.

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

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