1. 程式人生 > >[Java]LeetCode133. 克隆圖 | Clone Graph

[Java]LeetCode133. 克隆圖 | Clone Graph

Clone an undirected graph. Each node in the graph contains a label and a list of its neighbors.


OJ's undirected graph serialization:

Nodes are labeled uniquely.

We use # as a separator for each node, and , as a separator for node label and each neighbor of the node.

 

As an example, consider the serialized graph {0,1,2#1,2#2,2}.

The graph has a total of three nodes, and therefore contains three parts as separated by #.

  1. First node is labeled as 0. Connect node 0 to both nodes 1 and 2.
  2. Second node is labeled as 1
    . Connect node 1 to node 2.
  3. Third node is labeled as 2. Connect node 2 to node 2 (itself), thus forming a self-cycle.

 

Visually, the graph looks like the following:

       1
      / \
     /   \
    0 --- 2
         / \
         \_/

克隆一張無向圖,圖中的每個節點包含一個 label

 (標籤)和一個 neighbors (鄰接點)列表 。

OJ的無向圖序列化:

節點被唯一標記。

我們用 # 作為每個節點的分隔符,用 , 作為節點標籤和鄰接點的分隔符。

例如,序列化無向圖 {0,1,2#1,2#2,2}

該圖總共有三個節點, 被兩個分隔符  # 分為三部分。 

  1. 第一個節點的標籤為 0,存在從節點 0 到節點 1 和節點 2 的兩條邊。
  2. 第二個節點的標籤為 1,存在從節點 1 到節點 2 的一條邊。
  3. 第三個節點的標籤為 2,存在從節點 2 到節點 2 (本身) 的一條邊,從而形成自環。

我們將圖形視覺化如下:

       1
      / \
     /   \
    0 --- 2
         / \
         \_/

2ms
 1 /**
 2  * Definition for undirected graph.
 3  * class UndirectedGraphNode {
 4  *     int label;
 5  *     List<UndirectedGraphNode> neighbors;
 6  *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 7  * };
 8  */
 9 public class Solution {
10     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
11         if (node == null) return null;
12         Map<Integer, UndirectedGraphNode> map = new HashMap<>();  // 存放節點
13         return dfs(map, node);
14     }
15     
16      private  UndirectedGraphNode dfs(Map<Integer, UndirectedGraphNode> map, UndirectedGraphNode node) {
17         // 是否存在 存在返回
18         UndirectedGraphNode cloned = map.get(node.label);
19         if (cloned != null) return cloned;
20         // clone一個
21         cloned = new UndirectedGraphNode(node.label);
22         map.put(cloned.label, cloned);
23         // 加入子節點
24         for(UndirectedGraphNode neighbor: node.neighbors) {
25             cloned.neighbors.add(dfs(map,neighbor));
26         }
27         return cloned;
28     }
29 }

2ms

 1 /**
 2  * Definition for undirected graph.
 3  * class UndirectedGraphNode {
 4  *     int label;
 5  *     List<UndirectedGraphNode> neighbors;
 6  *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 7  * };
 8  */
 9 public class Solution {
10     HashMap<Integer, UndirectedGraphNode> map = new HashMap<>();
11     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
12         if(node==null)
13             return null;
14         
15         UndirectedGraphNode head = new UndirectedGraphNode(node.label);
16         map.put(head.label, head);
17        
18         for(UndirectedGraphNode nei: node.neighbors) {
19             // UndirectedGraphNode nei1 = null;
20             UndirectedGraphNode nei1 = map.get(nei.label);
21             if(nei1==null) {
22                 head.neighbors.add(cloneGraph(nei));
23             }
24             else
25                 head.neighbors.add(nei1);
26         }
27         return head;
28     }
29 }

4ms

 1 /**
 2  * Definition for undirected graph.
 3  * class UndirectedGraphNode {
 4  *     int label;
 5  *     List<UndirectedGraphNode> neighbors;
 6  *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 7  * };
 8  */
 9 public class Solution {
10     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
11         if (node == null) {
12             return null;
13         }
14         Map<UndirectedGraphNode, UndirectedGraphNode> visited = new HashMap<>();
15         visited.put(node, new UndirectedGraphNode(node.label));
16         dfs(node, visited);
17         return visited.get(node);
18     }
19     
20     private void dfs(UndirectedGraphNode node, Map<UndirectedGraphNode, UndirectedGraphNode> visited) {
21         for (UndirectedGraphNode nei : node.neighbors) {
22             if (!visited.containsKey(nei)) {
23                 visited.put(nei, new UndirectedGraphNode(nei.label));
24                 dfs(nei, visited);
25             }
26             visited.get(node).neighbors.add(visited.get(nei));
27         }    
28     
29     }
30 }

5ms

 1 /**
 2  * Definition for undirected graph.
 3  * class UndirectedGraphNode {
 4  *     int label;
 5  *     List<UndirectedGraphNode> neighbors;
 6  *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
 7  * };
 8  */
 9 public class Solution {
10     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
11         if(node == null) return null;
12         
13         Map<Integer, UndirectedGraphNode> valToNode = new HashMap<>();
14         Queue<UndirectedGraphNode> queue = new LinkedList<>();
15         queue.add(node);
16         
17         while(!queue.isEmpty()){
18             UndirectedGraphNode current = queue.poll();
19             UndirectedGraphNode copiedNode = valToNode.getOrDefault(current.label, new UndirectedGraphNode(current.label));
20             valToNode.put(current.label, copiedNode);
21             
22             for(UndirectedGraphNode neighbor : current.neighbors){
23                 UndirectedGraphNode copiedNeighbor = valToNode.getOrDefault(neighbor.label, new UndirectedGraphNode(neighbor.label));
24                 copiedNode.neighbors.add(copiedNeighbor);
25                 
26                 if(!valToNode.containsKey(neighbor.label)){
27                     queue.add(neighbor);
28                     valToNode.put(neighbor.label, copiedNeighbor);
29                 }
30             }
31         }
32         
33         return valToNode.get(node.label);
34     }
35 }