150 najlepszych wywiadówŚredni

Wykres klonowania

Szczegółowy przewodnik i implementacja Python dla problemu „Klonuj wykres”.

Oświadczenie o problemie

Średni

Biorąc pod uwagę odniesienie do węzła w połączonym grafie nieskierowanym, zwróć głęboką kopię (klon) grafu.

Każdy węzeł na wykresie zawiera wartość (int) i listę swoich sąsiadów (List[Node]).

Graf jest reprezentowany jako lista sąsiedztwa. Zaimplementuj funkcję cloneGraph(adjList: List[List[int]]) -> List[List[int]], gdzie adjList[i] reprezentuje sąsiadów węzła i+1 (indeks 1). Zwróć listę sąsiedztwa sklonowanego wykresu.

Ograniczenia
  • 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

Przykłady

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?
Rozważ użycie struktur danych specyficznych dla wykresów, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.