题目如下:

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, is it possible for you to finish all courses?

Example 1:

  1. Input: 2, [[1,0]]
  2. Output: true
  3. Explanation: There are a total of 2 courses to take.
  4.   To take course 1 you should have finished course 0. So it is possible.

Example 2:

  1. Input: 2, [[1,0],[0,1]]
  2. Output: false
  3. Explanation: There are a total of 2 courses to take.
  4.   To take course 1 you should have finished course 0, and to take course 0 you should
  5.   also have finished course 1. So it is impossible.

Note:

  1. The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
  2. You may assume that there are no duplicate edges in the input prerequisites.

解题思路:如果某一种输入课程无法被安排,那么一定存在至少这样一门课:通过BFS/DFS的方法从这门课开始,依次遍历需要安排在这门课后面的其他课,最终还会回到这门课,即组成了一个环。我们只有遍历所有的课,看看有没有哪门课会形成环路即可。

代码如下:

  1. class Solution(object):
  2. def canFinish(self, numCourses, prerequisites):
  3. """
  4. :type numCourses: int
  5. :type prerequisites: List[List[int]]
  6. :rtype: bool
  7. """
  8. dic = {}
  9. for cou,pre in prerequisites:
  10. if pre not in dic:
  11. dic[pre] = [cou]
  12. else:
  13. dic[pre].append(cou)
  14.  
  15. for i in range(numCourses):
  16. visit = [0] * numCourses
  17. queue = [i]
  18. start = None
  19. while len(queue) > 0:
  20. inx = queue.pop(0)
  21. if start == None:
  22. start = inx
  23. elif inx == start:
  24. return False
  25. if visit[inx] == 1:
  26. continue
  27. visit[inx] = 1
  28. if inx in dic and len(dic[inx]) > 0:
  29. queue += dic[inx]
  30. return True

【leetcode】207. Course Schedule的更多相关文章

  1. 【LeetCode】207. Course Schedule (2 solutions)

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

  2. 【LeetCode】207. Course Schedule 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/course-s ...

  3. 【LeetCode】210. Course Schedule II 解题报告(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 拓扑排序,BFS 拓扑排序,DFS 参考资料 日期 ...

  4. 【刷题-LeetCode】207. Course Schedule

    Course Schedule There are a total of numCourses courses you have to take, labeled from 0 to numCours ...

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

  6. 【LeetCode】代码模板,刷题必会

    目录 二分查找 排序的写法 BFS的写法 DFS的写法 回溯法 树 递归 迭代 前序遍历 中序遍历 后序遍历 构建完全二叉树 并查集 前缀树 图遍历 Dijkstra算法 Floyd-Warshall ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【Leetcode】Pascal's Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  9. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

随机推荐

  1. Gene Ontology (GO) 注释

    Gene Ontology (GO) 注释  Posted on 2017-06-11 |  In 生信 相似的基因在不同物种中,其功能往往保守的.显然,需要一个统一的术语用于描述这些跨物种的同源基因 ...

  2. Java Web学习总结(3)Servlet(二)

    一,Servlet访问URL映射配置 由于客户端是通过URL地址访问web服务器中的资源,所以Servlet程序若想被外界访问,必须把servlet程序映射到一个URL地址上,这个工作在web.xml ...

  3. 在Android中实现一个简易的Http服务器

    最近遇到一个需求需要在App中创建一个Http服务器供供浏览器调用,用了下开源的微型Htpp服务器框架:NanoHttpd,项目地址:https://github.com/NanoHttpd/nano ...

  4. 【Java】时间戳与Date相互转换

    时间戳转Date public static void main(String[] args) { // 10位的秒级别的时间戳 long time1 = 1527767665; String res ...

  5. 数据中 int 转 double 方式

    在mysql 中,得出一个int整数型数值 int整数值/int整数值   在被引用时,发现还是int类型 但是实际需要转换为 double 小数类型 查看相关函数,没有找到好的方法 后采用了 rou ...

  6. CF 39E. What Has Dirichlet Got to Do with That?(记忆化搜索+博弈论)

    传送门 解题思路 首先很好写出一个\(O(ab)\)的记搜,但发现这样无法处理\(a=1\)和\(b=1\)的情况,这两种情况需要特判.首先\(a=1\)的情况,就是如果当前选手让\(a+1\)必胜, ...

  7. __int128使用

    输入输出模板: __int128无法使用cin和cout进行输入输出,所以只能自己写一个输入输出的模板: #include <bits/stdc++.h> using namespace ...

  8. 测开之路四十七:Django之请求静态资源与模板

    框架必要的配置 import sysfrom django.conf.urls import urlfrom django.conf import settingsfrom django.http i ...

  9. Get The Treasury【HDU-3642】【扫描线】

    题目链接 题目给出的是N个体积块,问的是有多少体积重叠了3次及以上? 那么就是怎么处理体积这样子的问题了,看到Z的种类不多的时候,就想着从Z离散化的角度去考虑这个问题了,然后就是怎样子去处理面积了,这 ...

  10. C++中的函数重载分析(二)

    1,重载与指针: 1,下面的函数指针将保存哪个函数的地址? int func(int x) { return x; } int func(int a, int b) { return a + b; } ...