• LeetCode: Clone Graph


    这题的思路就是建一个old graph到新的graph点的对应map,queue表示还未访问过的old graph的点的队列,如果queue的front的点的neighbors vector里有未访问的点就添加到队列里。

     1 /**
     2  * Definition for undirected graph.
     3  * struct UndirectedGraphNode {
     4  *     int label;
     5  *     vector<UndirectedGraphNode *> neighbors;
     6  *     UndirectedGraphNode(int x) : label(x) {};
     7  * };
     8  */
     9 class Solution {
    10 public:
    11     UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
    12         // IMPORTANT: Please reset any member data you declared, as
    13         // the same Solution instance will be reused for each test case.
    14         if (node == NULL) return NULL;
    15         map<UndirectedGraphNode*, UndirectedGraphNode*> S;
    16         UndirectedGraphNode *res = new UndirectedGraphNode(node->label);
    17         S[node] = res;
    18         queue<UndirectedGraphNode *> que;
    19         que.push(node);
    20         while (!que.empty()) {
    21             UndirectedGraphNode *t = que.front();
    22             que.pop();
    23             for (int i = 0; i < t->neighbors.size(); i++) {
    24                 UndirectedGraphNode *nei = t->neighbors[i];
    25                 if (S.count(nei)) S[t]->neighbors.push_back(S[nei]);
    26                 else {
    27                     que.push(nei);
    28                     UndirectedGraphNode *tmp = new UndirectedGraphNode(nei->label);
    29                     S[nei] = tmp;
    30                     S[t]->neighbors.push_back(tmp);
    31                 }
    32             }
    33         }
    34         return res;
    35     }
    36 };

     C#

     1 /**
     2  * Definition for undirected graph.
     3  * public class UndirectedGraphNode {
     4  *     public int label;
     5  *     public IList<UndirectedGraphNode> neighbors;
     6  *     public UndirectedGraphNode(int x) { label = x; neighbors = new List<UndirectedGraphNode>(); }
     7  * };
     8  */
     9 public class Solution {
    10     public UndirectedGraphNode CloneGraph(UndirectedGraphNode node) {
    11         if (node == null) return null;
    12         Dictionary<UndirectedGraphNode, UndirectedGraphNode> S = new Dictionary<UndirectedGraphNode, UndirectedGraphNode>();
    13         UndirectedGraphNode ans = new UndirectedGraphNode(node.label);
    14         S.Add(node, ans);
    15         Queue<UndirectedGraphNode> que = new Queue<UndirectedGraphNode>();
    16         que.Enqueue(node);
    17         while (que.Count != 0) {
    18             UndirectedGraphNode peek = que.Peek();
    19             que.Dequeue();
    20             for (int i = 0; i < peek.neighbors.Count; i++) {
    21                 UndirectedGraphNode nei = peek.neighbors[i];
    22                 if (S.ContainsKey(nei)) S[peek].neighbors.Add(S[nei]);
    23                 else {
    24                     que.Enqueue(nei);
    25                     UndirectedGraphNode tmp = new UndirectedGraphNode(nei.label);
    26                     S.Add(nei, tmp);
    27                     S[peek].neighbors.Add(tmp);
    28                 }
    29             }
    30         }
    31         return ans;
    32     }
    33 }
    View Code
  • 相关阅读:
    开源APM应用性能管理工具调研
    Inside ARC — to see the code inserted by the compiler
    报表应用系统中怎样正确使用图表功能
    创建cifs系统案例之“实现将Windows磁盘共享至Linux”
    Eclipse快捷键 10个最有用的快捷键
    如何生成KeyStore
    android中调用系统的发送短信、发送邮件、打电话功能
    android自带theme
    Android 报错:Conversion to Dalvik format failed: Unable to execute dex: java.nio.BufferOverflowException. Check the Eclipse log for stack trace.
    Android oncreate onupgrade什么时候被调用
  • 原文地址:https://www.cnblogs.com/yingzhongwen/p/3404617.html
Copyright © 2020-2023  润新知