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

    https://oj.leetcode.com/problems/clone-graph/

    思路:dfs来复制,用一个hashmap标记已经处理过的节点防止无限循环。

    public class Solution {
        private HashMap<Integer, UndirectedGraphNode> map = new HashMap<Integer, UndirectedGraphNode>();
    
        public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
            map.clear();
            return clone(node);
        }
    
        private UndirectedGraphNode clone(UndirectedGraphNode node) {
            if (node == null)
                return null;
            if (map.containsKey(node.label))
                return map.get(node.label);
    
            UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
            map.put(node.label, copy);
    
            for (UndirectedGraphNode each : node.neighbors) {
                copy.neighbors.add(clone(each));
            }
    
            return copy;
    
        }
    
        public static void main(String[] args) {
            UndirectedGraphNode zero = new UndirectedGraphNode(0);
            UndirectedGraphNode one = new UndirectedGraphNode(1);
            UndirectedGraphNode two = new UndirectedGraphNode(2);
            zero.neighbors.add(one);
            zero.neighbors.add(two);
            one.neighbors.add(zero);
            one.neighbors.add(two);
            two.neighbors.add(zero);
            two.neighbors.add(one);
            two.neighbors.add(two);
    
            new Solution().cloneGraph(zero);
        }
    
    }

    第二遍记录:注意map的运用来终止无限循环,递归复制节点执行。

    /**
     * Definition for undirected graph.
     * class UndirectedGraphNode {
     *     int label;
     *     List<UndirectedGraphNode> neighbors;
     *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); }
     * };
     */
    public class Solution {
        Map<Integer,UndirectedGraphNode> map = new HashMap<Integer,UndirectedGraphNode>();
        public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
            if(node == null)
                return null;
            if(map.containsKey(node.label)){
                return map.get(node.label);
            }
            
            UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
            map.put(copy.label,copy);
            
            for(UndirectedGraphNode each:node.neighbors){
                copy.neighbors.add(cloneGraph(each));
            }
            return copy;   
        }
    }

    参考:

    http://www.cnblogs.com/lautsie/p/3356392.html

    http://blog.csdn.net/kenden23/article/details/18624191

    http://www.programcreek.com/2012/12/leetcode-clone-graph-java/

  • 相关阅读:
    值传递和引用传递(不是引用类型的传递)的区别
    字符串一旦定义,就表示开辟好了指定的空间,其内容就不可改变
    String类的直接赋值和构造方法赋值的区别
    字符串常量是String类的匿名对象
    Integer和int的区别(转)
    final的好处
    数组引用传递
    构造代码块
    ==和equals()的不同点
    Redis数据类型底层实现
  • 原文地址:https://www.cnblogs.com/jdflyfly/p/3828823.html
Copyright © 2011-2022 走看看