• Leetcod graph(new topic)


    797. All Paths From Source to Target

    1. class Solution {
    2. private:
    3. vectorint>> res;
    4. void traversal(vectorint>>& graph, vector<int>& path, int s){
    5. path.push_back(s);
    6. int n=graph.size();
    7. if(s == n-1){
    8. res.push_back(path);
    9. }
    10. for(auto v:graph[s]){
    11. traversal(graph, path, v);
    12. }
    13. path.pop_back();
    14. }
    15. public:
    16. vectorint>> allPathsSourceTarget(vectorint>>& graph) {
    17. vector<int> path;
    18. traversal(graph, path, 0);
    19. return res;

    类似于多叉树

    这里的s是记录正在走的节点

    如果不是acyclic(无环), 要用另外一个vector 记录走过的节点,防止进入死循环

    277. Find the Celebrity

    1. class Solution {
    2. public:
    3. int findCelebrity(int n) {
    4. int cand = 0;
    5. for(int other = 1; other < n; other++){
    6. if(knows(cand, other) || !knows(other, cand)){
    7. cand = other;
    8. }
    9. }
    10. for(int other = 0; other
    11. if(cand == other) continue;
    12. if(knows(cand, other) || !knows(other, cand)) return -1;
    13. }
    14. return cand;
    15. }
    16. };

    1.首先要排除,因为条件,所以至多有一个名人,那么两两对比,选出唯一的可能是名人的那个人

    2.对比的结果有四种

    3.最后剩下的那一个也不一定是名人,要在判断

    207. Course Schedule

    1.DFS

    1. class Solution {
    2. private:
    3. bool hasCycle = false;
    4. vector<bool> onPath;
    5. vector<bool> visited;
    6. vectorint>> buildGraph(int numCourses, vectorint>>& prerequisites){
    7. vectorint>> graph(numCourses);
    8. for(auto prerequisite:prerequisites){
    9. int from = prerequisite[1];
    10. int to = prerequisite[0];
    11. graph[from].push_back(to);
    12. }
    13. return graph;
    14. }
    15. void traversal(vectorint>>& graph, int s){
    16. if(onPath[s]){
    17. hasCycle = true;
    18. }
    19. if(visited[s] || hasCycle){
    20. return;
    21. }
    22. visited[s] = true;
    23. onPath[s] = true;
    24. for(auto t:graph[s]){
    25. traversal(graph, t);
    26. }
    27. onPath[s] = false;
    28. }
    29. public:
    30. bool canFinish(int numCourses, vectorint>>& prerequisites) {
    31. vectorint>> graph = buildGraph(numCourses, prerequisites);
    32. visited = vector<bool>(numCourses);
    33. onPath = vector<bool>(numCourses);
    34. for(int i=0; i
    35. traversal(graph, i);
    36. }
    37. return !hasCycle;
    38. }
    39. };

    1.首先要建一个graph表

    2.遍历判断是否有环

    210. Course Schedule II

    1. class Solution {
    2. private:
    3. vector<int> res;
    4. bool hasCycle;
    5. vector<bool> visited, onPath;
    6. void traversal(vectorint>>& graph, int s){
    7. if(onPath[s]){
    8. hasCycle = true;
    9. }
    10. if(visited[s] || hasCycle) return;
    11. onPath[s] = true;
    12. visited[s] = true;
    13. for(int t:graph[s]){
    14. traversal(graph, t);
    15. }
    16. res.push_back(s);
    17. onPath[s] = false;
    18. }
    19. vectorint>> buildGraph(int numCourses, vectorint>>& prerequisites){
    20. vectorint>> graph(numCourses);
    21. for(auto prerequisite:prerequisites){
    22. int from = prerequisite[1];
    23. int to = prerequisite[0];
    24. graph[from].push_back(to);
    25. }
    26. return graph;
    27. }
    28. public:
    29. vector<int> findOrder(int numCourses, vectorint>>& prerequisites) {
    30. vectorint>> graph = buildGraph(numCourses, prerequisites);
    31. visited = vector<bool>(numCourses, false);
    32. onPath = vector<bool>(numCourses, false);
    33. for(int i=0; i
    34. traversal(graph, i);
    35. }
    36. if(hasCycle) return {};
    37. reverse(res.begin(), res.end());
    38. return res;
    39. }
    40. };

    需要一个单独的vector记录

    这里用的是后序,所以之后要reverse一下

  • 相关阅读:
    广度优先(BFS)(例子:迷宫)
    基于kubernetes平台微服务的部署
    台灯到底对眼睛好不好?2022精选眼科医生推荐护眼灯
    《C Primer Plus》第12章复习题与编程练习
    Android Studio实现一个垃圾分类系统(Kotlin版本)
    绝对值和hashCode神奇的化学反应
    【Flutter】下载安装Flutter并使用学习dart语言
    基于微信小程序的知识题库系统设计与实现-计算机毕业设计源码+LW文档
    第五章 延迟计算
    Electron进程通信的另一种方式
  • 原文地址:https://blog.csdn.net/Zoeyii/article/details/133394717