• 【LeetCode】图论 graph(共20题)


    【133】Clone Graph (2019年3月9日,复习)

    给定一个图,返回它的深拷贝。

    题解:dfs 或者 bfs 都可以

     1 /*
     2 // Definition for a Node.
     3 class Node {
     4 public:
     5     int val;
     6     vector<Node*> neighbors;
     7 
     8     Node() {}
     9 
    10     Node(int _val, vector<Node*> _neighbors) {
    11         val = _val;
    12         neighbors = _neighbors;
    13     }
    14 };
    15 */
    16 class Solution {
    17 public:
    18     Node* cloneGraph(Node* node) {
    19         if (!node) {return node;}
    20         unordered_map<Node*, Node*> memo;
    21         Node* copy = new Node(node->val,  vector<Node*>{});
    22         memo[node] = copy;
    23         queue<Node*> que;
    24         que.push(node);
    25         unordered_set<Node*> visit;
    26         visit.insert(node);
    27         while (!que.empty()) {
    28             Node* cur = que.front(); que.pop();
    29             Node* copyCur = memo[cur];
    30             for (auto& adj : cur->neighbors) {
    31                 if (memo.find(adj) == memo.end()) {
    32                     Node* copyAdj = new Node(adj->val, vector<Node*>{});
    33                     memo[adj] = copyAdj;
    34                 }
    35                 copyCur->neighbors.push_back(memo[adj]);
    36                 if (visit.find(adj) == visit.end()) {
    37                     visit.insert(adj);
    38                     que.push(adj);
    39                 }
    40             }
    41         }
    42         return copy;
    43     }
    44 };
    View Code

    【207】Course Schedule 

    【210】Course Schedule II 

    【261】Graph Valid Tree 

    【269】Alien Dictionary 

    【310】Minimum Height Trees 

    【323】Number of Connected Components in an Undirected Graph 

    【332】Reconstruct Itinerary 

    【399】Evaluate Division 

    【444】Sequence Reconstruction 

    【684】Redundant Connection 

    【685】Redundant Connection II 

    【743】Network Delay Time 

    【765】Couples Holding Hands 

    【785】Is Graph Bipartite? 

    【802】Find Eventual Safe States 

    【839】Similar String Groups 

    【841】Keys and Rooms 

    【854】K-Similar Strings 

    【928】Minimize Malware Spread II 

  • 相关阅读:
    [codevs]线段树练习5
    【Java学习笔记之十六】浅谈Java中的继承与多态
    【Java学习笔记之十五】Java中的static关键字解析
    【Java学习笔记之十四】Java中this用法小节
    【Java学习笔记之十三】初探Java面向对象的过程及代码实现
    【Java学习笔记之十二】Java8增强的工具类:Arrays的用法整理总结
    hdu2896 AC自动机
    hdu2222 AC自动机
    字符串匹配--AC自动机模板
    字符串匹配--(K)MP模板
  • 原文地址:https://www.cnblogs.com/zhangwanying/p/9964164.html
Copyright © 2020-2023  润新知