• 力扣算法——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 };
  • 相关阅读:
    实战parse_ini_file()及扩展函数解析ini文件完整版
    Android应用程序注冊广播接收器(registerReceiver)的过程分析
    LeetCode: Best Time to Buy and Sell Stock II [122]
    关于URL编码
    js进阶 14-1 jquery的ajax系列中的load方法的作用是什么
    js进阶 13 jquery动画函数有哪些
    js进阶 13-11/12 jquery如何实现折叠导航
    js进阶 13-9/10 jquery如何实现三级列表
    js进阶 13-8 jquery如何实现侧边栏
    js进阶 13-7 如何实现滑动面板效果
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11839160.html
Copyright © 2020-2023  润新知