原题链接在这里:https://leetcode.com/problems/course-schedule-ii/

题目:

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 list of prerequisite pairs, return the ordering of courses you should take to finish all courses.

There may be multiple correct orders, you just need to return one of them. If it is impossible to finish all courses, return an empty array.

For example:

2, [[1,0]]

There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]

4, [[1,0],[2,0],[3,1],[3,2]]

There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].

题解:

用BFS based topological sort.  若是最后res的size 没有加到 numCourses, 说明没有可行方案,返回空的array.

若是可行,就把res转化成array.

Time Complexity: O(V + E). Space: O(V).

AC Java:

 public class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
List<Integer> res = new ArrayList<Integer>();
List<List<Integer>> graph = new ArrayList<List<Integer>>();
for(int i = 0; i<numCourses; i++){
graph.add(new ArrayList<Integer>());
}
for(int [] edge : prerequisites){
graph.get(edge[1]).add(edge[0]);
} int [] inDegree = new int[numCourses];
for(int [] edge : prerequisites){
inDegree[edge[0]]++;
} LinkedList<Integer> que = new LinkedList<Integer>();
for(int i = 0; i<inDegree.length; i++){
if(inDegree[i] == 0){
que.add(i);
}
} while(!que.isEmpty()){
int source = que.poll();
res.add(source); for(int dest : graph.get(source)){
inDegree[dest]--;
if(inDegree[dest] == 0){
que.add(dest);
}
}
} if(res.size() != numCourses){
return new int[0];
}
int [] resArr = new int[numCourses];
for(int i = 0; i<numCourses; i++){
resArr[i] = res.get(i);
}
return resArr;
}
}

类似Course Schedule.

跟上 Course Schedule III.

LeetCode Course Schedule II的更多相关文章

  1. [LeetCode] Course Schedule II 课程清单之二

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

  2. [Leetcode Week4]Course Schedule II

    Course Schedule II题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/course-schedule-ii/description/ De ...

  3. [LeetCode] Course Schedule I (207) & II (210) 解题思路

    207. Course Schedule There are a total of n courses you have to take, labeled from 0 to n - 1. Some ...

  4. 【LeetCode】210. Course Schedule II

    Course Schedule II There are a total of n courses you have to take, labeled from 0 to n - 1. Some co ...

  5. 【刷题-LeetCode】210. Course Schedule II

    Course Schedule II There are a total of n courses you have to take, labeled from 0 to n-1. Some cour ...

  6. [LeetCode] Course Schedule 课程清单

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

  7. [LeetCode] Course Schedule III 课程清单之三

    There are n different online courses numbered from 1 to n. Each course has some duration(course leng ...

  8. [LeetCode] Palindrome Partitioning II 解题笔记

    Given a string s, partition s such that every substring of the partition is a palindrome. Return the ...

  9. [leetcode]Word Ladder II @ Python

    [leetcode]Word Ladder II @ Python 原题地址:http://oj.leetcode.com/problems/word-ladder-ii/ 参考文献:http://b ...

随机推荐

  1. OFFICE 修改记录保存在单元格批注中vba

    Dim ydtext As String '原单元格值 Private Sub Worksheet_Change(ByVal Target As Range) If Target.Cells.Coun ...

  2. Swift -- 官方文档Swift-Guides的学习笔记

    在经历的一段时间的郁闷之后,我发现感情都是虚伪的,只有代码是真实的(呸) 因为看了swift语法之后依然不会用swift,然后我非常作死的跑去看官方文档,就是xcode里自带的help>docu ...

  3. Json 数据

    来自:极课学院 简介: json与xml json语法 json对象 json对象数组 用到的包 读取json数据例子 创建json数据

  4. 什么是java 键值对

    所谓键值对,你可以查看jdk文档,找MAP接口,它的实现类都是键值对的形式保存数据的 键:就是你存的值的编号值:就是你要存放的数据

  5. 安装Bind过程中提示丢失MSVCR110.dll的解决办法

    前几天在线安装Visual Studio 2012 Update 3,由于在线安装需要不断下载安装文件,时间很长,后来等不下去,就取消了,不幸的是VS启动不了了,弹出“devenv.exe – 系统错 ...

  6. SQL阻止保存要求重新创建表的更改 在哪里设置

    ef生成的数据表,有数据,设计的时候,想把某个字段改成可为null. 报 “阻止保存要求重新创建表”错误 百度一下: 修改数据库表结构时提示[不允许保存更改.您所做的更改要求删除并重新创建以下表.您对 ...

  7. mysql主从复制 主主复制 读写分离

    首先是mysql的主从复制很简单 主主复制也就是互相主从最麻烦的最难的就是日志恢复,增量恢复什么的比较复杂 首先如果你不会安装mysql版本最好一样,或者往上的版本,因为mysql是向下兼容 请注意不 ...

  8. fastjson生成json时Null属性不显示

    举个例子 生成JSON代码片段 Map < String , Object > jsonMap = new HashMap< String , Object>(); jsonM ...

  9. Javascript 笔记与总结(2-7)对象

    html: <h1>找对象</h1> <div id="div1"> <p>p1</p> <p>p2< ...

  10. the essence of the internet idea

    Computer Systems A Programmer's Perspective Second Edition Of course, we are glossing over many diff ...