leetcode-Course Schedule-207

有n个课程,输入一些二元组(x,y)表示先学习x必须先学习y,求根据输入的二元组的关系,能否修完所有课程

拓扑排序:选择一个入度为0的节点,删除该节点并且删除该节点所有的指出去的边,重复这个过程,知道没有入度为0的节点或者所有节点都被删除

bfs来做。不用bfs也行,模拟这个过程就行了

 1 class Solution {
 2 public:
 3     bool bfs(queue<int> q,vector<vector<int> > v,int len[],int num){
 4         while(!q.empty()){
 5             int tmp=q.front();
 6             q.pop();
 7             for(int i=0;i<v[tmp].size();i++){
 8                 len[v[tmp][i]]--;
 9                 if(len[v[tmp][i]]==0) q.push(v[tmp][i]);
10             }
11         }
12         for(int i=0;i<num;i++){
13             if(len[i]!=0) return false;
14         }
15         return true;
16     }
17     bool canFinish(int num, vector<pair<int, int> >& a) {
18         int len[num+1];
19         memset(len,0,sizeof(len));
20         vector<vector<int> > v(num+1);
21         for(int i=0;i<a.size();i++){
22             len[a[i].first]++;
23             v[a[i].second].push_back(a[i].first);
24         }
25         queue<int> q;
26         for(int i=0;i<num;i++){
27             if(len[i]==0)   q.push(i);
28         }
29         return bfs(q,v,len,num);
30     }
31 };
时间: 2024-08-10 01:55:49

leetcode-Course Schedule-207的相关文章

LeetCode Course Schedule II

原题链接在这里:https://leetcode.com/problems/course-schedule-ii/ 是Course Schedule的进阶版题目.采用的是同一种方法,这里只需要返回一个结果,所以遍历过的节点放入array中即可. AC Java: 1 public class Solution { 2 public int[] findOrder(int numCourses, int[][] prerequisites) { 3 int [] res = new int[num

[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 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 l

[LeetCode] Course Schedule 课程清单

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 l

[LeetCode] Course Schedule

As suggested by the hints, this problem is equivalent to detecting a cycle in the graph represented by prerequisites. Both BFS and DFS can be used to solve this problem using the idea oftopological sort. If you find yourself unfamiliar with these con

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

There are n different online courses numbered from 1 to n. Each course has some duration(course length) tand closed on dth day. A course should be taken continuously for t days and must be finished before or on the dth day. You will start at the 1st 

[LeetCode]Course Schedule,解题报告

题目 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

LeetCode编程训练 - 拓扑排序(Topological Sort)

拓扑排序基础 拓扑排序用于解决有向无环图(DAG,Directed Acyclic Graph)按依赖关系排线性序列问题,直白地说解决这样的问题:有一组数据,其中一些数据依赖其他,问能否按依赖关系排序(被依赖的排在前面),或给出排序结果. 最常用解决拓扑排序问题的方法是Kahn算法,步骤可以概括为: 1. 根据依赖关系,构建邻接矩阵或邻接表.入度数组 2. 取入度为0的数据(即不依赖其他数据的数据),根据邻接矩阵/邻接表依次减小依赖其的数据的入度 3. 判断减小后是否有新的入度为0的数据,继续进

Solution to LeetCode Problem Set

Here is my collection of solutions to leetcode problems. LeetCode - Course Schedule LeetCode - Reverse Linked List LeetCode - Isomorphic Strings LeetCode - Count Primes LeetCode - Remove Linked List Elements LeetCode - Happy Number LeetCode - Bitwise

199. Binary Tree Right Side View

题目: Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. For example:Given the following binary tree, 1 <--- / 2 3 <--- \ 5 4 <--- You should return [1, 3,

leetcode 207. Course Schedule

https://leetcode.com/problems/course-schedule/#/description 图的算法题 题目就是求一堆课程有先修和后修的区别,这些课程能否构成拓扑排序而不矛盾: 应该算是我做的第一个关于图的算法题,一开始毫无思路,看了网上的sloutions,发现这道题还是比较简单的,可以用bfs和dfs都可以做.感觉还是比较典型的关于图算法例题. http://www.cnblogs.com/zmyvszk/p/5573234.html 这个题解讲的还是比较明白的,