zoukankan      html  css  js  c++  java
  • 力扣算法——133.CloneGraph【M】

    Given a reference of a node in a connected undirected graph, return a deep copy (clone) of the graph. Each node in the graph contains a val (int) and a list (List[Node]) of its neighbors.

    Example:

    Input:
    {"$id":"1","neighbors":[{"$id":"2","neighbors":[{"$ref":"1"},{"$id":"3","neighbors":[{"$ref":"2"},{"$id":"4","neighbors":[{"$ref":"3"},{"$ref":"1"}],"val":4}],"val":3}],"val":2},{"$ref":"4"}],"val":1}
    
    Explanation:
    Node 1's value is 1, and it has two neighbors: Node 2 and 4.
    Node 2's value is 2, and it has two neighbors: Node 1 and 3.
    Node 3's value is 3, and it has two neighbors: Node 2 and 4.
    Node 4's value is 4, and it has two neighbors: Node 1 and 3.
    

    Note:

    1. The number of nodes will be between 1 and 100.
    2. The undirected graph is a simple graph, which means no repeated edges and no self-loops in the graph.
    3. Since the graph is undirected, if node p has node q as neighbor, then node q must have node p as neighbor too.
    4. You must return the copy of the given node as a reference to the cloned graph.

    Soulution:

      使用DFS和BFS即可

      两种方法在leetcode中都能通过,但在牛客中,BFS使用的而外空间超出限制,所以只能使用DFS

      

     1 struct UndirectedGraphNode {
     2     int label;
     3     vector<UndirectedGraphNode *> neighbors;
     4     UndirectedGraphNode(int x) : label(x) {};
     5 };
     6 
     7 //DFS
     8 class Solution01 {
     9 public:
    10     UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
    11         unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>mapR;
    12         return helper(node, mapR);
    13     }
    14     UndirectedGraphNode* helper(UndirectedGraphNode*node, unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>&mapR)
    15     {
    16         if (node == nullptr)return nullptr;
    17         if (mapR.find(node) != mapR.end())//旧节点
    18             return mapR[node];
    19         UndirectedGraphNode* res = new UndirectedGraphNode(node->label);//新节点
    20         mapR[node] = res;
    21         for (auto a : node->neighbors)
    22             res->neighbors.push_back(helper(a, mapR));
    23         return res;
    24     }
    25 };
    26 //BFS
    27 class Solution02 {
    28 public:
    29     UndirectedGraphNode *cloneGraph(UndirectedGraphNode *node) {
    30         queue<UndirectedGraphNode*>qNode;
    31         qNode.push(node);
    32         unordered_map<UndirectedGraphNode*, UndirectedGraphNode*>mapR;
    33         UndirectedGraphNode *res = new UndirectedGraphNode(node->label);
    34         mapR[node] = res;
    35         while (!qNode.empty())
    36         {
    37             UndirectedGraphNode *p = qNode.front();
    38             qNode.pop();
    39             for (auto a : p->neighbors)
    40             {
    41                 if (mapR.find(a) == mapR.end())//新节点
    42                 {
    43                     qNode.push(a);//加入
    44                     mapR[a] = new UndirectedGraphNode(a->label);//新节点
    45                 }
    46                 mapR[p]->neighbors.push_back(mapR[a]);//连接节点
    47             }
    48         }
    49         return res;        
    50     }
    51 };
  • 相关阅读:
    POJ 1953 World Cup Noise
    POJ 1995 Raising Modulo Numbers (快速幂取余)
    poj 1256 Anagram
    POJ 1218 THE DRUNK JAILER
    POJ 1316 Self Numbers
    POJ 1663 Number Steps
    POJ 1664 放苹果
    如何查看DIV被设置什么CSS样式
    独行DIV自适应宽度布局CSS实例与扩大应用范围
    python 从入门到精通教程一:[1]Hello,world!
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11839160.html
Copyright © 2011-2022 走看看