zoukankan      html  css  js  c++  java
  • [LeetCode-JAVA] 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 list of prerequisite pairs, is it possible for you to finish all courses?

    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 it is possible.

    2, [[1,0],[0,1]]

    There are a total of 2 courses to take. To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.

    思路:首先要里解题意 一些课程有先修课程--这些课程必须先学,类似下面的图片,这个就是典型的有向图的拓扑排序

    步骤1:找到一个入度为零的顶点

    步骤2:从图中删除这个顶点

    步骤3:循环1、2 如果所有顶点都走过了 那么就是一个可以完成的图

    其次还要了解题中算法的输入 numCourse为要选的课的总数 而prerequisite矩阵是先决条件矩阵(开始一直以为是邻接矩阵)

    所以,第一步要先将给出的矩阵,换算出邻接矩阵(也可以是邻接链表),然后再步步进行。

    代码:

    public class Solution {
        public boolean canFinish(int numCourses, int[][] prerequisites) {
            int[] indegree = new int[numCourses];
            int[][] matrix = new int[numCourses][numCourses];  // [i][j] i为j的先决条件 i->j
            Stack<Integer> stack = new Stack<Integer>();
            
            for(int i = 0 ; i < prerequisites.length ; i++){
                if(matrix[prerequisites[i][1]][prerequisites[i][0]] == 1) continue;//输入有重复
                indegree[prerequisites[i][0]]++; //入度加一
                matrix[prerequisites[i][1]][prerequisites[i][0]] = 1; //p中[j]<-[i]
            }
            
            for(int i = 0 ; i < numCourses ; i++){
                if(indegree[i] == 0)    //将入度为零的压入栈
                    stack.push(i);
            }
            
            while(!stack.isEmpty()){
                int temp = stack.pop();  //删掉入度为零的点所有连出的线 
                for(int i = 0 ; i < numCourses ; i++){  //每删掉一个 将对应的入度减一
                    if(matrix[temp][i] == 1){
                        matrix[temp][i] = 0;
                        indegree[i]--;   //如果减去后i对应的入度为0 则将其入栈
                        if(indegree[i] == 0)
                            stack.push(i);
                    }
                }
            }
            
            for(int i = 0 ; i < numCourses ; i++){   //判断是否有入度不为零的点
                if(indegree[i] > 0)
                    return false;
            }
            
            return true;
        }
    }
  • 相关阅读:
    windows安装pkg-config
    完美解决Cannot download "https://github.com/sass/node-sass/releases/download/binding.nod的问题
    iOS 解析RFC3339时间格式
    依赖工程开发,编译报错Command Libtool failed with a nonzero exit code
    移除项目中的UIWebView
    输出手机中的字体与常用的苹方字体名称
    Null passed to a callee that requires a non-null argument
    SDK内本地化处理 localizedStringForKey:value:table:
    iOS [AFHTTPSessionManager GET:parameters:progress:success:failure:]: unrecognized selector sent to
    xcode搜索路径缩写
  • 原文地址:https://www.cnblogs.com/TinyBobo/p/4544860.html
Copyright © 2011-2022 走看看