zoukankan      html  css  js  c++  java
  • 133. 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
             / 
             \_/

    这道题目还是比较难的,用DFS和BFS都可以做出来,首先说DFS,dfs的终止条件就是把第一个节点的所有邻居节点全部遍历到就算终止了,然后每次遍历邻居节点的时候,邻居节点的所有邻居也会
    遍历到,注意本题的判断是否node为空只是一种特殊情况,代码如下:
     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     Map<Integer,UndirectedGraphNode> map = new HashMap<>();
    11     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
    12         if(node==null) return null;
    13         if(map.containsKey(node.label)){
    14             return map.get(node.label);
    15         }
    16         UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
    17         map.put(node.label,copy);
    18         for(UndirectedGraphNode un:node.neighbors){
    19             copy.neighbors.add(cloneGraph(un));
    20         }
    21         return copy;
    22     }
    23 }

     本题还可以用BFS来做,想法是使用一个queue来存储node以及neighbor,hashmap来存储copy的node和neighbor,代码如下:

     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         UndirectedGraphNode copy = new UndirectedGraphNode(node.label);
    14         map.put(copy.label,copy);
    15         LinkedList<UndirectedGraphNode> queue= new LinkedList<>();
    16         queue.add(node);
    17         while(!queue.isEmpty()){
    18             UndirectedGraphNode n = queue.pop();
    19             for(UndirectedGraphNode neighbor:n.neighbors){
    20                 if(!map.containsKey(neighbor.label)){
    21                     map.put(neighbor.label,new UndirectedGraphNode(neighbor.label));
    22                     queue.add(neighbor);
    23                 }
    24                 map.get(n.label).neighbors.add(map.get(neighbor.label));
    25             }
    26         }
    27         return copy;
    28     }
    29 }
  • 相关阅读:
    leetcode 48. Rotate Image
    leetcode 203. Remove Linked List Elements 、83. Remove Duplicates from Sorted List 、82. Remove Duplicates from Sorted List II(剑指offer57 删除链表中重复的结点) 、26/80. Remove Duplicates from Sorted ArrayI、II
    leetcode 263. Ugly Number 、264. Ugly Number II 、313. Super Ugly Number 、204. Count Primes
    leetcode 58. Length of Last Word
    安卓操作的一些问题解决
    leetcode 378. Kth Smallest Element in a Sorted Matrix
    android studio Gradle Build速度加快方法
    禁用gridview,listview回弹或下拉悬停
    Android Studio找不到FragmentActivity类
    安卓获取ListView、GridView等滚动的距离(高度)
  • 原文地址:https://www.cnblogs.com/codeskiller/p/6553610.html
Copyright © 2011-2022 走看看