题目如下:

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:

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

Example 2:

Input: 2, [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
  To take course 1 you should have finished course 0, and to take course 0 you should
  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的方法从这门课开始,依次遍历需要安排在这门课后面的其他课,最终还会回到这门课,即组成了一个环。我们只有遍历所有的课,看看有没有哪门课会形成环路即可。

代码如下:

class Solution(object):
def canFinish(self, numCourses, prerequisites):
"""
:type numCourses: int
:type prerequisites: List[List[int]]
:rtype: bool
"""
dic = {}
for cou,pre in prerequisites:
if pre not in dic:
dic[pre] = [cou]
else:
dic[pre].append(cou) for i in range(numCourses):
visit = [0] * numCourses
queue = [i]
start = None
while len(queue) > 0:
inx = queue.pop(0)
if start == None:
start = inx
elif inx == start:
return False
if visit[inx] == 1:
continue
visit[inx] = 1
if inx in dic and len(dic[inx]) > 0:
queue += dic[inx]
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. vue中使用canvas绘制签名

    不多说,上代码: <template>         <div class="sign-canvas">             <canvas   ...

  2. Xcode7.1环境下上架iOS App到AppStore 流程①

    前言部分 之前App要上架遇到些问题到网上搜上架教程发现都是一些老的版本的教程 ,目前iTunesConnect 都已经迭代好几个版本了和之前的 界面风格还是有很大的差别的,后面自己折腾了好久才终于把 ...

  3. ajax使用jsonp请求方式

    /* //简写形式,效果相同 $.getJSON("http://app.example.com/base/json.do?sid=1494&busiId=101&jsonp ...

  4. 英语单词deprecated

    deprecated 来源——fdisk /dev/sdb [root@centos65 ~]# fdisk /dev/sdb WARNING: DOS-compatible mode is depr ...

  5. WIN10无法识别安卓设备,提示Windows 无法验证此设备所需的驱动程序的数字签名

    在设备管理器,显示ANDROID设备是感叹号, 不管更新驱动,还是下载什么手机助手自动安装驱动,均不可解. 从属性中查看提示的是“Windows 无法验证此设备所需的驱动程序的数字签名”, 解决办法: ...

  6. 使用kindeditor直接粘贴本地图片或者是qq截图

    我司需要做一个需求,就是使用富文本编辑器时,不要以上传附件的形式上传图片,而是以复制粘贴的形式上传图片. 在网上找了一下,有一个插件支持这个功能. WordPaster 安装方式如下: 直接使用Wor ...

  7. C# 调用windows时间同步服务获取准确时间

    //创建一个Daytime类代码如下:using System; using System.Collections; using System.Collections.Generic; using S ...

  8. 前端每日实战:46# 视频演示如何用纯 CSS 创作一个在容器中反弹的小球

    效果预览 按下右侧的"点击预览"按钮可以在当前页面预览,点击链接可以全屏预览. https://codepen.io/comehope/pen/jKVbyE 可交互视频教程 此视频 ...

  9. wsl和windows相互访问文件夹

    How to access Windows folders from Bash on Ubuntu on Windows You'll find the Windows C:\ structure a ...

  10. net core 发布docker镜像的官方写法

    使用vscode发布的镜像可能存在不能运行的问题, 可以去docker的官方文档下找一个标准的格式再替换一下就可以用了: FROM mcr.microsoft.com/dotnet/core/sdk: ...