[LeetCode]Course Schedule II
来源:互联网 发布:淘宝物品拍摄技巧 编辑:程序博客网 时间:2024/06/10 10:25
这次是上次Course Schedule的后续...Course Schedule II
210Course Schedule II26.6%Medium说是后续...其实在Course Schedule就已经把II完成了80%了orz(好的真的挺后续的...
题目要求是这样的:
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]
.
Note:
- The input prerequisites is a graph represented by a list of edges, not adjacency matrices. Read more about how a graph is represented.
- You may assume that there are no duplicate edges in the input prerequisites.
代码如下:
#include<iostream>#include<vector>#include<queue>#include<algorithm>using namespace std;struct Node{int num;vector<Node*> next;int indegree;bool ischeck;Node(int n){num=n;ischeck=false;indegree=0;}};vector<Node*> TopologicalSort(int numCourses,vector<Node*>& graph){queue<Node*> list;vector<Node*> sortedList;int count;for(int i=0;i<numCourses;i++){if(graph[i]->indegree==0&&graph[i]->ischeck==false){list.push(graph[i]);}}while(list.size()>0){Node* temp=list.front();//cout<<"temp is "<<temp->num;sortedList.push_back(list.front());temp->ischeck=true;//cout<<" "<<temp->num<<"'s ischeck is "<<temp->ischeck<<endl;list.pop();for(int i=0;i<temp->next.size();i++){temp->next[i]->indegree--;if(temp->next[i]->indegree==0){ list.push(temp->next[i]); //cout<<"list pushback "<<temp->next[i]->num<<endl;}}}if(sortedList.size()==numCourses){return sortedList;}else{ vector<Node*> temp; return temp;}}vector<int> findOrder(int numCourses, vector<pair<int, int>>& prerequisites){ vector<int> answer; /* if(prerequisites.size()==0) { return answer; } *///queue<Node*> tocheck;vector<Node*> graph;for(int i=0;i<numCourses;i++){Node* temp=new Node(i);graph.push_back(temp);}int size=prerequisites.size();for(int i=0;i<size;i++){int first=prerequisites[i].first;int second=prerequisites[i].second;graph[second]->next.push_back(graph[first]);graph[first]->indegree++;}vector<Node*> finish=TopologicalSort(numCourses,graph);for(int i=0;i<finish.size();i++){ answer.push_back(finish[i]->num);}return answer;}刚开始没考虑太仔细,没有改太多,结果就忘了如果给定的条件转化出来的是个有环图该怎么返回,接下来就是如果课程修读顺序没有具体要求又该返回什么,这些一开始都没注意到,然后还是跑了一遍test code才发现的(尴尬
- [leetcode]Course Schedule II
- leetcode - Course Schedule II
- LeetCode Course Schedule II
- [leetcode] Course Schedule II
- leetcode:Course Schedule II
- [LeetCode] Course Schedule II
- Leetcode Course Schedule II
- 【leetcode】Course Schedule II
- leetcode Course Schedule II
- LeetCode Course Schedule II
- [leetcode] Course Schedule II
- #leetcode#Course Schedule II
- LeetCode Course Schedule II
- leetcode Course Schedule II
- Course Schedule II -- leetcode
- Leetcode: Course Schedule II
- [Leetcode] Course Schedule II
- LeetCode -- Course Schedule II
- 【Oracle 】截取字符函数:SUBSTR()与检索字符位置函数:INSTR()实例
- servlet生命周期与工作原理
- 李白打酒
- 自己用ssh框架写了个小项目中的一些总结
- jni崩溃输出
- [LeetCode]Course Schedule II
- Android 7.0 Gallery图库源码分析3
- 昨天终于把《VISUAL C++ 2010入门经典_第5版(中文版)》看完一遍了
- css初步学习1——css格式,文字处理属性,颜色属性
- Action 跳转的方法和详解
- c语言中逗号运算符的作用
- Mybatis where in语句如何传参数
- 网络抓包工具Fiddler的使用
- 十条笑话:结账时要320元,于是我和老板讲价,说给300行不