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]; for(int i=; i<n; i++) {
map[ prerequisites[i][] ] ++;
} Queue<Integer> que = new LinkedList<Integer>();
int index = numCourses-;
for(int i=; i<numCourses; i++) {
if(map[i] == ) {
que.add(i);
res[index--] = i;
}
} while(!que.isEmpty() ){
int k = que.remove();
for(int i=; i<n; i++) {
int l = prerequisites[i][];
if(k==prerequisites[i][]) {
map[l]--;
if(map[l] == ) {
que.add(l);
res[index--] = l;
}
}
}
}
if(index!=-) return new int[];
else return res;
}
}

补充一个python的实现,使用深度优先遍历,进行拓扑排序:

 class Solution:
def dfs(self,visited,memo,dic,i,postCourse):
if visited[i]:
return True
if memo[i] == :
return False
elif memo[i] == :
return True
for cs in dic[i]:
visited[i] = True
bl = self.dfs(visited,memo,dic,cs,postCourse)
visited[i] = False
if bl:
memo[i] =
return True
memo[i] =
postCourse.append(i)
return False
def findOrder(self, numCourses: int, prerequisites: 'List[List[int]]') -> 'List[int]':
dic = {}
n = len(prerequisites)
for i in range(numCourses):
dic[i] = []
for i in range(n):
cs = prerequisites[i][]#当前课程
pre = prerequisites[i][]#前置课程
dic[cs].append(pre)
visited = [False for _ in range(numCourses)]
memo = [- for _ in range(numCourses)]
postCourse = []
for i in range(numCourses):
if self.dfs(visited,memo,dic,i,postCourse):
return []
return postCourse

leetcode210的更多相关文章

  1. [Swift]LeetCode210. 课程表 II | Course Schedule II

    There are a total of n courses you have to take, labeled from 0 to n-1. Some courses may have prereq ...

  2. leetcode210.拓扑排序

    拓扑排序能否成功,其实就是看有没有环 有环:说明环内结点互为前置,永远也不可能完成 无环:是线性的,可以完成 DFS方法 思路: 逆向思维,遍历到边界点(无邻接点相当于叶子),再不断回溯将结点加入到结 ...

随机推荐

  1. 关于递归函数中的return位置

    1.对于求是否有解的问题一般使用bool dfs()  其中return 可以放在递归式后面 2.对于需要更新解的问题一般使用int dfs()  其中return 不能放在递归式后面,必须放在函数最 ...

  2. stenciljs 学习十 服务器端渲染

      stenciljs提供了 ssr 支持,对于express 最简单的就是使用提供的中间件 express 集成 const express = require('express'); const ...

  3. 微软通过.NET Native为Windows Store应用提速

    .NET Native是微软的一次尝试,旨在降低Windows Store应用的启动时间和内存占用. 自从去年11月份,有人发现Windows Store应用的启动速度有了大幅提高后,对该项目的猜测就 ...

  4. EasyPHP-Devserver-17的坑位

    mysql登陆错误:error: 'Plugin '*2A8AF30E682613A2F1CE1E28BA11D8560B294DCE' is not loaded' http://stackover ...

  5. RTSP HTTP RTP RTCP

    RTSP简介 RTSP(Real Time Streaming Protocol)是由Real Network和Netscape共同提出的如何有效地在IP网络上传输流媒体数据的应用层协议.RTSP对流 ...

  6. hadoop之 exceeds the limit of concurrent xcievers处理

    dfs.datanode.max.transfer.threads: 默认 4096 < 2.0之前该参数为dfs.datanode.max.xcievers >解释:Specifies ...

  7. Array、ArrayList 区别

    ArrayList可以算是Array的加强版,(对array有所取舍的加强). 存储内容比较(可包含元素的类型不同.数组要求存储同种类型): Array数组可以包含基本类型和对象类型, ArrayLi ...

  8. markdown 知识点

    符号 说明 作用 ___ 三个下划线 一条直线 * 或_ 1个星号 或 1个下划线 文字斜体 ** 或__ 2个星号 或 2个下划线 文字加粗 全角2个空格 缩进2个汉字 竖线之间加3个间隔符放在第二 ...

  9. 注意字符串的strlen与sizeof的差别

    unsigned char AT_RESET[]="r\r\n"; printf("strlen=%d sizeof=%d\n",strlen(AT_RESET ...

  10. JS Web的Flex弹性盒子模型

    1. justify-content <!DOCTYPE html> <html lang="en"> <head> <meta char ...