【leetcode】Clone Graph

来源:互联网 发布:桌面办公软件 编辑:程序博客网 时间:2024/05/17 04:37

题目:

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


思路:


首先做一次BFS遍历,将所有结点创建出来。并且要保存在一个可以在短时间读取的结构中。map是个不错的结构并且每个结点label唯一,因此我们可以用label来作为索引。

第二次遍历的时候可以快速地找出结点并构造图的关系。

代码:

[java] view plaincopy
  1. /** 
  2.  * Definition for undirected graph. 
  3.  * class UndirectedGraphNode { 
  4.  *     int label; 
  5.  *     ArrayList<UndirectedGraphNode> neighbors; 
  6.  *     UndirectedGraphNode(int x) { label = x; neighbors = new ArrayList<UndirectedGraphNode>(); } 
  7.  * }; 
  8.  */  
  9. public class Solution {  
  10.     Map<Integer, UndirectedGraphNode> nodeMap = new HashMap<Integer, UndirectedGraphNode>();  
  11.     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {  
  12.         // Note: The Solution object is instantiated only once and is reused by each test case.  
  13.         if (node == null) {  
  14.             return null;  
  15.         }  
  16.           
  17.         LinkedList<UndirectedGraphNode> stack = new LinkedList<UndirectedGraphNode>();  
  18.         //Map<Integer, UndirectedGraphNode> nodeMap = new HashMap<Integer, UndirectedGraphNode>();  
  19.         nodeMap.clear();  
  20.         Set<Integer> isFound = new HashSet<Integer>();  
  21.           
  22.         stack.addLast(node);  
  23.           
  24.         UndirectedGraphNode head = null;  
  25.           
  26.         while (!stack.isEmpty()) {  
  27.             UndirectedGraphNode first = stack.getFirst();  
  28.             stack.removeFirst();  
  29.               
  30.             int label = first.label;  
  31.               
  32.             UndirectedGraphNode n = nodeMap.get(label);  
  33.             if (n == null) {  
  34.                 n = new UndirectedGraphNode(label);  
  35.                 nodeMap.put(label, n);  
  36.             }  
  37.               
  38.             if (head == null) {  
  39.                 head = n;  
  40.             }  
  41.               
  42.             if (!isFound.contains(label)) {  
  43.                 isFound.add(label);  
  44.                   
  45.                 ArrayList<UndirectedGraphNode> neighbors = first.neighbors;  
  46.                   
  47.                 for (UndirectedGraphNode nb : neighbors) {  
  48.                     UndirectedGraphNode nn = nodeMap.get(nb.label);  
  49.                     if (nn == null) {  
  50.                         nn = new UndirectedGraphNode(nb.label);  
  51.                         nodeMap.put(nb.label, nn);  
  52.                           
  53.                         stack.addLast(nb);  
  54.                     }  
  55.                     n.neighbors.add(nn);                }  
  56.             }  
  57.               
  58.               
  59.         }  
  60.           
  61.         return head;  
  62.     }  
  63.   
  64. }


0 0
原创粉丝点击