leetcode 210. Course Schedule II 拓扑排序 + HashSet

来源:互联网 发布:淘宝搜索链接代码 编辑:程序博客网 时间:2024/05/22 13:05

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

和上一道题题意完全一样,不过要打印一条可能的拓扑排序的结果,所以直接输出即可。

要和这一道题leetcode 207. Course Schedule 课程调度 + 拓扑排序 一起学习。

代码如下:

import java.util.ArrayList;import java.util.Arrays;import java.util.HashSet;import java.util.Iterator;import java.util.LinkedList;import java.util.List;import java.util.Queue;import java.util.Set;/* * 本质就是判断图中是否有环 * 拓扑排序的应用 * http://blog.csdn.net/ljiabin/article/details/45846837 * 和上面的一题完全一样,只是打印一下输出 * */public class Solution {    public int[] findOrder(int numCourses, int[][] prerequisites)    {               int []preCount=new int[numCourses];        List<Set<Integer>> list=new ArrayList<Set<Integer>>();        Arrays.fill(preCount, 0);        for(int i=0;i<numCourses;i++)            list.add(new HashSet<>());        /*         * list保存的是出度边,也即后继结点         * 这里使用set是为了避免重复的边         * perCount保存的说前驱节点的数量         * */        for(int i=0;i<prerequisites.length;i++)        {            int to=prerequisites[i][0];            int from=prerequisites[i][1];            list.get(from).add(to);            preCount[to]++;        }        //使用队列来拓扑排序        Queue<Integer> queue=new LinkedList<Integer>();        for(int i=0;i<numCourses;i++)        {            if(preCount[i]==0)                queue.add(i);        }        List<Integer> resList=new ArrayList<>();        while(queue.isEmpty()==false)        {            int from=queue.poll();            resList.add(from);            Set<Integer> one=list.get(from);            Iterator<Integer> iter =one.iterator();            while(iter.hasNext())            {                int to=iter.next();                preCount[to]--;                if(preCount[to]==0)                    queue.add(to);            }        }        if(resList.size()==numCourses)        {            int []order=new int [numCourses];            for(int i=0;i<numCourses;i++)                order[i]=resList.get(i);            return order;        }else            return new int[0];    }}

下面是C++的做法,和上一道题的做法一抹一眼,就是做一个拓扑排序

代码如下:

#include <iostream>#include <vector>#include <queue>#include <stack>#include <string>#include <set>#include <map>using namespace std;class Solution{public:    vector<int> findOrder(int numCourses, vector<pair<int, int>>& reque)    {        if (numCourses <= 0)            return vector<int>();        if (numCourses == 1)            return vector<int>(1, 0);        vector<int> res;        vector<int> preCount(numCourses, 0);        vector<set<int>> next(numCourses, set<int>());        for (pair<int, int> key : reque)        {            int from = key.second;            int to = key.first;            next[from].insert(to);            preCount[to]++;        }        queue<int> que;        for (int i = 0; i < preCount.size(); i++)        {            if (preCount[i] == 0)                que.push(i);        }        while (que.empty() == false)        {            int from = que.front();            que.pop();            res.push_back(from);            for (set<int>::iterator i = next[from].begin(); i != next[from].end(); i++)            {                preCount[*i]--;                if (preCount[*i] == 0)                    que.push(*i);            }        }        return res.size() == numCourses ? res : vector<int>();    }};
原创粉丝点击