zoukankan      html  css  js  c++  java
  • 210. 课程表 II (JAVA)

    现在你总共有 n 门课需要选,记为 0 到 n-1。

    在选修某些课程之前需要一些先修课程。 例如,想要学习课程 0 ,你需要先完成课程 1 ,我们用一个匹配来表示他们: [0,1]

    给定课程总量以及它们的先决条件,返回你为了学完所有课程所安排的学习顺序。

    可能会有多个正确的顺序,你只要返回一种就可以了。如果不可能完成所有课程,返回一个空数组。

    示例 1:

    输入: 2, [[1,0]]
    输出: [0,1]
    解释: 总共有 2 门课程。要学习课程 1,你需要先完成课程 0。因此,正确的课程顺序为 [0,1] 。
    示例 2:

    输入: 4, [[1,0],[2,0],[3,1],[3,2]]
    输出: [0,1,2,3] or [0,2,1,3]
    解释: 总共有 4 门课程。要学习课程 3,你应该先完成课程 1 和课程 2。并且课程 1 和课程 2 都应该排在课程 0 之后。
      因此,一个正确的课程顺序是 [0,1,2,3] 。另一个正确的排序是 [0,2,1,3] 。
    说明:

    输入的先决条件是由边缘列表表示的图形,而不是邻接矩阵。详情请参见图的表示法。
    你可以假定输入的先决条件中没有重复的边。
    提示:

    这个问题相当于查找一个循环是否存在于有向图中。如果存在循环,则不存在拓扑排序,因此不可能选取所有课程进行学习。
    通过 DFS 进行拓扑排序 - 一个关于Coursera的精彩视频教程(21分钟),介绍拓扑排序的基本概念。
    拓扑排序也可以通过 BFS 完成。

    思路:

    本题实质是对有向无环图的拓扑排序。

    拓扑排序的方法步骤:

    1. 整理每个节点的后续节点

    2. 计算每个点的入度,将入度为0的点放入result,并将其后续节点的入度-1

    3. 依此类推

    实现方法可以是DFS(通过栈)或是BFS(通过队列)

    方法I:BFS。出队列一个,就将它后续节点中入度为0的节点放入队列 (for循环包 + while循环)

    class Solution {
        public int[] findOrder(int numCourses, int[][] prerequisites) {
            int[] result = new int[numCourses];
            int rIndex = 0; //result的下标
            int[] pathIn = new int[numCourses]; //默认初始化值为0
            Queue<Integer> noPathInQueue = new LinkedList<Integer>();
            Map<Integer, List<Integer>> followersMap = new HashMap<>();
            List<Integer> followers;
            int index;
            
            //遍历prerequisites,构建followersMap和pathIn
            for(int i = 0; i < prerequisites.length; i++){
                if(followersMap.containsKey(prerequisites[i][1])){
                    followers = followersMap.get(prerequisites[i][1]);
                }
                else {
                    followers = new ArrayList<Integer>();
                }
                followers.add(prerequisites[i][0]);
                followersMap.put(prerequisites[i][1], followers);
                pathIn[prerequisites[i][0]]++;
            }
    
            //遍历寻找入度为0的点
            for(int i = 0; i < numCourses; i++){ 
                if(pathIn[i]==0) {
                    noPathInQueue.offer(i); //队列满时返回false;add()则会直接抛出异常
                    
                }
            }
    
            //进行拓扑排序
            while(!noPathInQueue.isEmpty()) {
                index = noPathInQueue.peek();
                result[rIndex++] = index;
                noPathInQueue.poll();
                pathIn[index] = -1; //表示已添加到队列
                for(int j = 0; followersMap.containsKey(index) && j < followersMap.get(index).size(); j++){ //将followers的入度-1
                    pathIn[followersMap.get(index).get(j)]--;
                    if(pathIn[followersMap.get(index).get(j)] == 0){
                        noPathInQueue.offer(followersMap.get(index).get(j)); 
                    }
                }
            } 
    
            if ( rIndex != numCourses) {
                return new int[0];
            }
            return result;
    
        }
    }

    方法II:DFS。出栈一个,就将它后续节点中入度为0的节点入栈,只要栈不为空就还停留在当前for循环中(for循环包while循环)

    class Solution {
        public int[] findOrder(int numCourses, int[][] prerequisites) {
            int[] result = new int[numCourses];
            int rIndex = 0; //result的下标
            int[] pathIn = new int[numCourses]; //默认初始化值为0
            Stack<Integer> noPathInStack = new Stack<Integer>();
            Map<Integer, List<Integer>> followersMap = new HashMap<>();
            List<Integer> followers;
            int index;
            
            //遍历prerequisites,构建followersMap和pathIn
            for(int i = 0; i < prerequisites.length; i++){
                if(followersMap.containsKey(prerequisites[i][1])){
                    followers = followersMap.get(prerequisites[i][1]);
                }
                else {
                    followers = new ArrayList<Integer>();
                }
                followers.add(prerequisites[i][0]);
                followersMap.put(prerequisites[i][1], followers);
                pathIn[prerequisites[i][0]]++;
            }
    
            //进行拓扑排序
            for(int i = 0; i < numCourses; i++){ //遍历寻找入度为0的点
                if(pathIn[i]==0) {
                    noPathInStack.push(i); 
                    pathIn[i] = -1; //表示已添加到栈
                }
    
                while(!noPathInStack.isEmpty()){
                    index = noPathInStack.peek();
                    result[rIndex++] = index;
                    noPathInStack.pop();
                    for(int j = 0; followersMap.containsKey(index) && j < followersMap.get(index).size(); j++){ //将followers的入度-1
                        pathIn[followersMap.get(index).get(j)]--;
                        if(pathIn[followersMap.get(index).get(j)] == 0) {
                            noPathInStack.push(followersMap.get(index).get(j));
                            pathIn[followersMap.get(index).get(j)] = -1;
                        }
                    }
                }
            }
    
            if ( rIndex != numCourses) {
                return new int[0];
            }
            return result;
        }
    }
  • 相关阅读:
    南大《软件分析》课程笔记——Intermediate Representation
    leetcode刷题
    shiro550反序列化分析
    南大《软件分析》课程笔记——第一课
    再见2020
    S2-002漏洞分析
    编码&解码工具
    正码、反码和补码
    Java数据结构(二)
    Java数据集合
  • 原文地址:https://www.cnblogs.com/qionglouyuyu/p/13418296.html
Copyright © 2011-2022 走看看