IT技術小屋

          秋風秋雨,皆入我心

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            38 隨筆 :: 1 文章 :: 19 評論 :: 0 Trackbacks
          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搜索,其中Set<UndirectedGraphNode> visited對已經訪問過的節點進行記錄,Map<UndirectedGraphNode, UndirectedGraphNode> map用來記錄新老節點的對應關系。代碼實現如下:
           1 import java.util.HashMap;
           2 import java.util.HashSet;
           3 import java.util.LinkedList;
           4 import java.util.Map;
           5 import java.util.Queue;
           6 import java.util.Set;
           7 
           8 public class CloneGraph {
           9     public UndirectedGraphNode cloneGraph(UndirectedGraphNode node) {
          10         if (node == null)
          11             return node;
          12         Queue<UndirectedGraphNode> queue = new LinkedList<UndirectedGraphNode>();
          13         queue.add(node);
          14         Set<UndirectedGraphNode> visited = new HashSet<UndirectedGraphNode>();
          15         Map<UndirectedGraphNode, UndirectedGraphNode> map = new HashMap<UndirectedGraphNode, UndirectedGraphNode>();
          16         while (!queue.isEmpty()) {
          17             UndirectedGraphNode n = queue.remove();
          18             if (visited.contains(n))
          19                 continue;
          20             visited.add(n);
          21             UndirectedGraphNode clone;
          22             if (!map.containsKey(n)) {
          23                 clone = new UndirectedGraphNode(n.label);
          24                 map.put(n, clone);
          25             } else {
          26                 clone = map.get(n);
          27             }
          28             for (UndirectedGraphNode child : n.neighbors) {
          29                 queue.add(child);
          30                 UndirectedGraphNode cloneChild;
          31                 if (!map.containsKey(child)) {
          32                     cloneChild = new UndirectedGraphNode(child.label);
          33                     map.put(child, cloneChild);
          34                 } else {
          35                     cloneChild = map.get(child);
          36                 }
          37                 clone.neighbors.add(cloneChild);
          38             }
          39         }
          40         return map.get(node);
          41     }
          42 }
          posted on 2013-12-19 10:36 Meng Lee 閱讀(979) 評論(0)  編輯  收藏 所屬分類: Leetcode
          主站蜘蛛池模板: 增城市| 囊谦县| 老河口市| 繁峙县| 正镶白旗| 丹巴县| 睢宁县| 巴林左旗| 武汉市| 安吉县| 从江县| 集安市| 肥东县| 荔浦县| 兴安县| 楚雄市| 大荔县| 汤阴县| 保康县| 竹北市| 杂多县| 朝阳县| 大城县| 金湖县| 澎湖县| 广汉市| 获嘉县| 东海县| 观塘区| 古浪县| 黔江区| 高要市| 赣榆县| 加查县| 独山县| 高雄县| 莱芜市| 札达县| 牡丹江市| 元谋县| 张北县|