原题链接在这里: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. linux用户和组管理

    添加组groupadd sftp 把用户mysftp加入组sftp中:gpasswd -a mysftp sftp 把用户mysftp加入组sftp中:usermod -a -G sftp mysft ...

  2. 菜刀轻松砍杀安全狗 asp一句话中转脚本

    看到很多朋友看了我的PHP中转脚本http://phpinfo.me/2014/02/01/309.html ,问我那个脚本只能中转PHP的,但是asp的呢 asp连接的时候安全狗拦截的正是菜刀POS ...

  3. PHP wget 增强脱裤脚本(PDO MYSQL)

    脚本参考了 LCX Gavin2位前辈的帖子.在此表示非常的感谢. https://www.t00ls.net/thread-26740-1-1.html https://www.t00ls.net/ ...

  4. DirSync: List of attributes that are synced by the Azure Active Directory Sync Tool

    http://social.technet.microsoft.com/wiki/contents/articles/19901.dirsync-list-of-attributes-that-are ...

  5. 在Excel中实现查询功能

    $sn = Read-Host -Prompt "请输入员工号|序列号|资产号" $xl = New-Object -ComObject "Excel.Applicati ...

  6. 学习SSH框架

    1.SSH框架的认知 在做相关的java的网页的开发制作时,良好的层次分解是十分有比要的,所以我们在云涌第三方的框架之下来简化还有名了我们相关的网站的开发. SSH框架实则为Struct + spri ...

  7. OpenCV学习笔记——图像的腐蚀与膨胀

    顺便又复习了一下cvcopy如何进行图像拼接(最近觉得打开多幅图像分别看不如缩小掉放拼接到一幅图像上对比来的好) 首先把拼接的目标图像设置兴趣区域ROI,比如我有一个total,要把a.b.c分别从左 ...

  8. HDU 2181 哈密顿绕行世界问题(经典DFS+回溯)

    哈密顿绕行世界问题 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. 移动Web应用开发入门指南——交互篇

    交互篇 从PC到移动端,视觉和交互是用户能直接感受到的差异.在视觉篇中已经提到,移动设备的物理属性一部分影响到视觉,另外一些部分将影响到交互.那么,移动设备影响交互的物理属性都有哪些变化呢?对于这个问 ...

  10. 利用Excel画柱状图,并且包含最大最小值

    如何利用Excel画出如上样式的图? 1.绘制柱状图.如何绘制柱状图,操作非常简单,选中数据,点击合适的图表样式即可. 2.添加误差线.选中已绘制好的图,添加误差线.如果误差线没有出现,可以使用”更多 ...