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

  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.
相比实验一返回的是bool值true or false这次要求的返回值是course的修读顺序...那不很简单吗,不就是拓扑排序的结果嘛。这下刚好,Course Schedule还用了个vector把拓扑排序的结果存起来了,这下子刚好可以直接用上了。不过在一次循环中因为入度为0的点可能不止一个,所以拓扑排序的结果可能会有多种,但是顺序不会影响到课程的修读结果,也就是说,输出的正确结果可以有多个,只要符合题目要求就可以了。以我的算法,是课程编号小的优先修读。

代码如下:

#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才发现的(尴尬
0 0
原创粉丝点击