zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    oc之数组反序输出示例
    OC--有这么一个 整数 123456789,如何将这个整数的每一位数,从末位开始依次放入数组中,并遍历 倒序输出字符串
    oc--截取字符串(从网址中截取用户名和密码)
    iOS 第七期考核题(字符串以及字典的使用 数组内容转换成字符串)
    iOS 第六期考核题(字典的使用)
    iOS 第五期考核题(字典与数组嵌套,字典的排序/删除)
    Linux服务启动报错日志分析
    新手Linux命令-1
    新手Linux命令-2
    计划任务服务
  • 原文地址:https://www.cnblogs.com/lishiblog/p/4202653.html
Copyright © 2011-2022 走看看