Leetcod graph(new topic)

797. All Paths From Source to Target

cpp 复制代码
class Solution {
private:
    vector<vector<int>> res;
    void traversal(vector<vector<int>>& graph, vector<int>& path, int s){
        path.push_back(s);
        int n=graph.size();
        if(s == n-1){
            res.push_back(path);
        }
        for(auto v:graph[s]){
            traversal(graph, path, v);
        }
        path.pop_back();
    }
public:
    vector<vector<int>> allPathsSourceTarget(vector<vector<int>>& graph) {
        vector<int> path;
        traversal(graph, path, 0);
        return res;
   

类似于多叉树

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

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

277. Find the Celebrity

cpp 复制代码
class Solution {
public:
    int findCelebrity(int n) {
        int cand = 0;
        for(int other = 1; other < n; other++){
            if(knows(cand, other) || !knows(other, cand)){
                cand = other;
            }
        }

        for(int other = 0; other<n; other++){
            if(cand == other) continue;
            if(knows(cand, other) || !knows(other, cand)) return -1;
        }
        return cand;
    }
};

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

2.对比的结果有四种

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

207. Course Schedule

1.DFS

cpp 复制代码
class Solution {
private:
    bool hasCycle = false;
    vector<bool> onPath;
    vector<bool> visited;
    vector<vector<int>> buildGraph(int numCourses, vector<vector<int>>& prerequisites){
        vector<vector<int>> graph(numCourses);
        for(auto prerequisite:prerequisites){
            int from = prerequisite[1];
            int to = prerequisite[0];
            graph[from].push_back(to);
        }
        return graph;
    }
    void traversal(vector<vector<int>>& graph, int s){
        if(onPath[s]){
            hasCycle = true;
        }
        if(visited[s] || hasCycle){
            return;
        }
        visited[s] = true;
        onPath[s] = true;
        for(auto t:graph[s]){
            traversal(graph, t);
        }
        onPath[s] = false;
    }
public:
    bool canFinish(int numCourses, vector<vector<int>>& prerequisites) {
        vector<vector<int>> graph = buildGraph(numCourses, prerequisites);
        visited = vector<bool>(numCourses);
        onPath = vector<bool>(numCourses);
        for(int i=0; i<numCourses; i++){
            traversal(graph, i);
        }
        return !hasCycle;
    }
};

1.首先要建一个graph表

2.遍历判断是否有环

210. Course Schedule II

cpp 复制代码
class Solution {
private:
    vector<int> res;
    bool hasCycle;
    vector<bool> visited, onPath;

    void traversal(vector<vector<int>>& graph, int s){
        if(onPath[s]){
            hasCycle = true;
        }
        if(visited[s] || hasCycle) return;
        onPath[s] = true;
        visited[s] = true;

        for(int t:graph[s]){
            traversal(graph, t);
        }
        res.push_back(s);
        onPath[s] = false;
    }

    vector<vector<int>> buildGraph(int numCourses, vector<vector<int>>& prerequisites){
        vector<vector<int>> graph(numCourses);
        for(auto prerequisite:prerequisites){
            int from = prerequisite[1];
            int to = prerequisite[0];
            graph[from].push_back(to);
        }
        return graph;
    }
public:
    vector<int> findOrder(int numCourses, vector<vector<int>>& prerequisites) {
        vector<vector<int>> graph = buildGraph(numCourses, prerequisites);
        visited = vector<bool>(numCourses, false);
        onPath = vector<bool>(numCourses, false);
        for(int i=0; i<numCourses; i++){
            traversal(graph, i);
        }
        if(hasCycle) return {};
        reverse(res.begin(), res.end());
        return res;
    }
};

需要一个单独的vector记录

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

相关推荐
张人玉7 分钟前
C# 常量与变量
java·算法·c#
学不动CV了24 分钟前
数据结构---链表结构体、指针深入理解(三)
c语言·arm开发·数据结构·stm32·单片机·链表
weixin_446122461 小时前
LinkedList剖析
算法
百年孤独_2 小时前
LeetCode 算法题解:链表与二叉树相关问题 打打卡
算法·leetcode·链表
我爱C编程2 小时前
基于拓扑结构检测的LDPC稀疏校验矩阵高阶环检测算法matlab仿真
算法·matlab·矩阵·ldpc·环检测
算法_小学生2 小时前
LeetCode 75. 颜色分类(荷兰国旗问题)
算法·leetcode·职场和发展
运器1233 小时前
【一起来学AI大模型】算法核心:数组/哈希表/树/排序/动态规划(LeetCode精练)
开发语言·人工智能·python·算法·ai·散列表·ai编程
算法_小学生3 小时前
LeetCode 287. 寻找重复数(不修改数组 + O(1) 空间)
数据结构·算法·leetcode
岁忧3 小时前
(LeetCode 每日一题) 1865. 找出和为指定值的下标对 (哈希表)
java·c++·算法·leetcode·go·散列表
alphaTao3 小时前
LeetCode 每日一题 2025/6/30-2025/7/6
算法·leetcode·职场和发展