天天看點

【LeetCode】133. Clone Graph 克隆圖(Medium)(JAVA)

【LeetCode】133. Clone Graph 克隆圖(Medium)(JAVA)

題目位址: https://leetcode.com/problems/clone-graph/

題目描述:

Given a reference of a node in a connected undirected graph.

Return a deep copy (clone) of the graph.

Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors.

class Node {
    public int val;
    public List<Node> neighbors;
}
           

Test case format:

For simplicity sake, each node’s value is the same as the node’s index (1-indexed). For example, the first node with val = 1, the second node with val = 2, and so on. The graph is represented in the test case using an adjacency list.

Adjacency list is a collection of unordered lists used to represent a finite graph. Each list describes the set of neighbors of a node in the graph.

The given node will always be the first node with val = 1. You must return the copy of the given node as a reference to the cloned graph.

Example 1:

【LeetCode】133. Clone Graph 克隆圖(Medium)(JAVA)
Input: adjList = [[2,4],[1,3],[2,4],[1,3]]
Output: [[2,4],[1,3],[2,4],[1,3]]
Explanation: There are 4 nodes in the graph.
1st node (val = 1)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
2nd node (val = 2)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
3rd node (val = 3)'s neighbors are 2nd node (val = 2) and 4th node (val = 4).
4th node (val = 4)'s neighbors are 1st node (val = 1) and 3rd node (val = 3).
           

Example 2:

【LeetCode】133. Clone Graph 克隆圖(Medium)(JAVA)
Input: adjList = [[]]
Output: [[]]
Explanation: Note that the input contains one empty list. The graph consists of only one node with val = 1 and it does not have any neighbors.
           

Example 3:

Input: adjList = []
Output: []
Explanation: This an empty graph, it does not have any nodes.
           

Example 4:

【LeetCode】133. Clone Graph 克隆圖(Medium)(JAVA)
Input: adjList = [[2],[1]]
Output: [[2],[1]]
           

Constraints:

  • 1 <= Node.val <= 100
  • Node.val is unique for each node.
  • Number of Nodes will not exceed 100.
  • There is no repeated edges and no self-loops in the graph.
  • The Graph is connected and all nodes can be visited starting from the given node.

題目大意

給你無向 連通 圖中一個節點的引用,請你傳回該圖的 深拷貝(克隆)。

圖中的每個節點都包含它的值 val(int) 和其鄰居的清單(list[Node])。

class Node {
    public int val;
    public List<Node> neighbors;
}
           

測試用例格式:

簡單起見,每個節點的值都和它的索引相同。例如,第一個節點值為 1(val = 1),第二個節點值為 2(val = 2),以此類推。該圖在測試用例中使用鄰接清單表示。

鄰接清單 是用于表示有限圖的無序清單的集合。每個清單都描述了圖中節點的鄰居集。

給定節點将始終是圖中的第一個節點(值為 1)。你必須将 給定節點的拷貝 作為對克隆圖的引用傳回。

提示:

  1. 節點數不超過 100 。
  2. 每個節點值 Node.val 都是唯一的,1 <= Node.val <= 100。
  3. 無向圖是一個簡單圖,這意味着圖中沒有重複的邊,也沒有自環。
  4. 由于圖是無向的,如果節點 p 是節點 q 的鄰居,那麼節點 q 也必須是節點 p 的鄰居。
  5. 圖是連通圖,你可以從給定節點通路到所有節點。

解題方法

  1. 這一題的主要問題是存在環,如果不解開環的話,必然會循環調用
  2. 為了解開環,因為題目裡指明了 node.val 是獨一無二的,用一個 map 來存儲,key 值就是 node.val,在往下周遊前,先把目前元素存入 map 中
class Solution {
    Map<Integer, Node> map = new HashMap<>();
    public Node cloneGraph(Node node) {
        if (node == null) return null;
        if (map.get(node.val) != null) return map.get(node.val);
        ArrayList<Node> neighbors = new ArrayList<>();
        map.put(node.val, new Node(node.val, neighbors));
        for (int i = 0; i < node.neighbors.size(); i++) {
            neighbors.add(cloneGraph(node.neighbors.get(i)));
        }
        return map.get(node.val);
    }
}
           

執行耗時:33 ms,擊敗了97.89% 的Java使用者

記憶體消耗:38.7 MB,擊敗了95.85% 的Java使用者

歡迎關注我的公衆号,LeetCode 每日一題更新
【LeetCode】133. Clone Graph 克隆圖(Medium)(JAVA)