leetcode210】的更多相关文章

There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1] Given the total number of courses and a lis…
public class Solution { //test case [1,0] public int[] findOrder(int numCourses, int[][] prerequisites) { int[] map = new int[numCourses]; int n = prerequisites.length; int[] res = new int[numCourses]; ; i<n; i++) { map[ prerequisites[i][] ] ++; } Qu…
拓扑排序能否成功,其实就是看有没有环 有环:说明环内结点互为前置,永远也不可能完成 无环:是线性的,可以完成 DFS方法 思路: 逆向思维,遍历到边界点(无邻接点相当于叶子),再不断回溯将结点加入到结果中,得到的是拓扑排序的逆序,进行反转即可得到拓扑序列. 遍历过程中判断是否有环. 注意:要使用vist[]标记三种状态.假如只标记两种状态,则下面这种情况会判定为false,但其实是true 代码 class Solution { // 找到出度为0的点, vector<int> res; ve…