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:

  1. 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]

  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].

解题思路:

参考上题,Java for LeetCode 207 Course Schedule【Medium】

修改下代码即可,JAVA实现如下:

  1. public int[] findOrder(int numCourses, int[][] prerequisites) {
  2. int[] res=new int[numCourses];
  3. List<Set<Integer>> posts = new ArrayList<Set<Integer>>();
  4. for (int i = 0; i < numCourses; i++)
  5. posts.add(new HashSet<Integer>());
  6. for (int i = 0; i < prerequisites.length; i++)
  7. posts.get(prerequisites[i][1]).add(prerequisites[i][0]);
  8.  
  9. // count the pre-courses
  10. int[] preNums = new int[numCourses];
  11. for (int i = 0; i < numCourses; i++) {
  12. Set<Integer> set = posts.get(i);
  13. Iterator<Integer> it = set.iterator();
  14. while (it.hasNext()) {
  15. preNums[it.next()]++;
  16. }
  17. }
  18.  
  19. // remove a non-pre course each time
  20. for (int i = 0; i < numCourses; i++) {
  21. // find a non-pre course
  22. int j = 0;
  23. for ( ; j < numCourses; j++) {
  24. if (preNums[j] == 0) break;
  25. }
  26.  
  27. // if not find a non-pre course
  28. if (j == numCourses) return new int[0];
  29. res[i]=j;
  30. preNums[j] = -1;
  31.  
  32. // decrease courses that post the course
  33. Set<Integer> set = posts.get(j);
  34. Iterator<Integer> it = set.iterator();
  35. while (it.hasNext()) {
  36. preNums[it.next()]--;
  37. }
  38. }
  39. return res;
  40. }

Java for LeetCode 210 Course Schedule II的更多相关文章

  1. LeetCode 210. Course Schedule II(拓扑排序-求有向图中是否存在环)

    和LeetCode 207. Course Schedule(拓扑排序-求有向图中是否存在环)类似. 注意到.在for (auto p: prerequistites)中特判了输入中可能出现的平行边或 ...

  2. [LeetCode] 210. 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 ...

  3. Leetcode 210 Course Schedule II

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

  4. [LeetCode] 210. Course Schedule II 课程安排II

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

  5. (medium)LeetCode 210.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 ...

  6. [leetcode]210. Course Schedule II课程表II

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

  7. 【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 ...

  8. 【刷题-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 ...

  9. Java for LeetCode 207 Course Schedule【Medium】

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

随机推荐

  1. Android Studio-设置快速修复错误提示代码

    File-Settings-keyMap-show intention actions.

  2. 该不该用inline-block取代float? inline和float的区别?

    该不该用inline-block取代float? 请看这篇文章引用: jtyjty99999的博客 让块级元素 水平排列的通常方式是float, 但是float可能会带来很多意外的问题 可以考虑用in ...

  3. 理解Memcached的分布式

    Memcached尽管是"分布式"的缓存系统,但是服务器端并没有分布式功能.各个Memcached实例不会相互通信以共享信息,Memcached如何进行分布式完全取决于客户端的实现 ...

  4. 开始使用pycharm了

    我将python的主力开发工具从eclipse+pydev切换到pycharm社区版了. 选择pycharm 的原因:1. pycharm可以实时按照pep8的规范检查code style和namin ...

  5. "当前方法的代码已经过优化,无法计算表达式的值"的这个错误的解决方案!!!

    http://blog.useasp.net/archive/2012/09/12/how-to-debug-dotnet-framework-source-when-throw-the-code-o ...

  6. soj4271 Love Me, Love My Permutation (DFS)

    4271: Love Me, Love My Permutation Description Given a permutation of n: a[0], a[1] ... a[n-1], ( it ...

  7. 【Go入门教程8】总结(25个关键字)

    这一章我们主要介绍了Go语言的一些语法,通过语法我们可以发现Go是多么的简单,只有二十五个关键字.让我们再来回顾一下这些关键字都是用来干什么的. break    default      func  ...

  8. bootstrap-tab

    功能:点击时切换相应的内容或图片 插件:tab.js 要点:tab标签用在导航条上,以data-toggle作被点击者, 以tab-content作内容显示 <!DOCTYPE html> ...

  9. Object.prototype.toString.call()进行类型判断

    为什么类型判断用到Object.prototype.toString.call()进行类型判断,而不用typeof()呢? 然后翻了一下资料: Typeof 在使用 ]));/));));//[obj ...

  10. php字符串格式化函数addslashes()

    1.这个函数的使用和php.ini中的magic_quotes_gpc的配置有关,默认情况下,这个配置为on.并且,这个配置处于一个较高级别,脚本中不能修改.所以,检测这个配置情况就很重要. 2.在脚 ...