• LeetCode-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
             / 
             \_/
    

    Solution:
     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<UndirectedGraphNode,UndirectedGraphNode> cloneMap = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
    14         Set<UndirectedGraphNode> visited = new HashSet<UndirectedGraphNode>();
    15         cloneMap.put(node, new UndirectedGraphNode(node.label)); 
    16         
    17         return cloneGraphRecur(node,cloneMap,visited);
    18     }
    19 
    20     public UndirectedGraphNode cloneGraphRecur(UndirectedGraphNode cur, Map<UndirectedGraphNode,UndirectedGraphNode> cloneMap, Set<UndirectedGraphNode> visited){
    21         UndirectedGraphNode curClone = cloneMap.get(cur);
    22         visited.add(cur);
    23 
    24         //Clone all the neighbors of cur node.
    25         for (int i=0;i<cur.neighbors.size();i++){
    26             UndirectedGraphNode neigh = cur.neighbors.get(i);
    27             if (cloneMap.containsKey(neigh)){
    28                 curClone.neighbors.add(cloneMap.get(neigh));
    29             } else {
    30                 UndirectedGraphNode neighClone = new UndirectedGraphNode(neigh.label);
    31                 cloneMap.put(neigh,neighClone);
    32                 curClone.neighbors.add(neighClone);
    33             }
    34         }
    35 
    36         //Visit all unvisited neighbors of cur node.
    37         for (int i=0;i<cur.neighbors.size();i++)
    38             if (!visited.contains(cur.neighbors.get(i)))
    39                 cloneGraphRecur(cur.neighbors.get(i),cloneMap,visited);
    40 
    41         return curClone;
    42     }
    43             
    44 }

    Solution (BFS):

     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<UndirectedGraphNode,UndirectedGraphNode> cloneMap = new HashMap<UndirectedGraphNode,UndirectedGraphNode>();
    14         List<UndirectedGraphNode> visited = new ArrayList<UndirectedGraphNode>();
    15         cloneMap.put(node, new UndirectedGraphNode(node.label)); 
    16         visited.add(node);
    17         int index = 0;
    18         while (index<visited.size()){
    19             UndirectedGraphNode cur = visited.get(index);
    20             UndirectedGraphNode curClone = cloneMap.get(cur);
    21 
    22             //Clone all the neighbors of cur node.
    23             for (int i=0;i<cur.neighbors.size();i++){
    24                 UndirectedGraphNode neigh = cur.neighbors.get(i);
    25             if (cloneMap.containsKey(neigh)){
    26                     curClone.neighbors.add(cloneMap.get(neigh));
    27                 } else {
    28                     UndirectedGraphNode neighClone = new UndirectedGraphNode(neigh.label);
    29                     cloneMap.put(neigh,neighClone);
    30                 curClone.neighbors.add(neighClone);
    31                 visited.add(neigh);
    32                 }
    33             }
    34 
    35             index++;
    36         }
    37         
    38         return cloneMap.get(node);
    39 
    40 
    41     }            
    42 }
  • 相关阅读:
    XML文件处理
    前端技术学习路线及技术汇总
    Install wget for mac
    AT&T Assembly for Linux and Mac (sys_write)
    AT&T Assembly for Linux and Mac (sys_exit)
    [leetCode]5. 最长回文子串(DP)
    [深入理解JVM虚拟机]第3章-垃圾收集器、内存分配策略
    [深入理解JVM虚拟机]第2章-Java内存区域与内存溢出异常
    [LeetCode]695. 岛屿的最大面积(DFS/BFS)、200. 岛屿数量(DFS/BFS待做/并差集待做)
    [LeetCode]415. 字符串相加
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4202653.html
Copyright © 2020-2023  润新知