Course Schedule II:判断有向图是否有环

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.

Example 1:

Input: 2, [[1,0]] 
Output: [0,1]
Explanation: 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] .

Example 2:

Input: 4, [[1,0],[2,0],[3,1],[3,2]]
Output: [0,1,2,3] or [0,2,1,3]
Explanation: 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.

思路1:判断每个顶点的入度,每次将入度为0的顶点移除,直至所有顶点均被移除,否则图有环。

思路2:深度优先遍历每个顶点,若访问期间再次访问到正在被当前遍历访问中的顶点,则图有环。visited表示是否正在被访问,discover表示该顶点是否已被深度优先遍历过。(相当于用三种颜色表示节点:未被访问、正在被访问、已经访问完毕三种状态。)

思路二代码:来自:https://leetcode.com/problems/course-schedule-ii/discuss/133808/Java-DFS-beats-99.

class Solution {
    
    boolean[] discovered;
    boolean[] visited;
    int[] resultSet;
    int counter=0;
    
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        
        // Construct adjList first
        List<Integer>[] adjList = constructAdjList(numCourses, prerequisites);
       
        discovered = new boolean[numCourses];
        visited = new boolean[numCourses];
        
        resultSet = new int[numCourses];
        
        for(int i=0; i<numCourses; i++) {
            
            if(!discovered[i]) {
                
                if(adjList[i] == null){
                    discovered[i] = true;
                    resultSet[counter++] = i; // Courses which don't have any prereq can be added in any order.
                    continue;
                }
                
                if(findCycle(i, adjList)) {
                    return new int[0];
                }
                
            }
            
        }
        
        return resultSet;
        
    }
    
    private List<Integer>[] constructAdjList(int n, int[][] prereq) {
        
        List<Integer>[] adjList = new ArrayList[n];
        
        for(int i=0; i<prereq.length; i++) {
            
            int vertex = prereq[i][0];
            int neighbor = prereq[i][1];
            
            if(adjList[vertex] == null) {
                adjList[vertex] = new ArrayList<Integer>();
            }
            
            adjList[vertex].add(neighbor);
            
        }
        
        return adjList;
        
    }
    
    private boolean findCycle(int vertex, List<Integer>[] adjList) {
        
        discovered[vertex] = true;
        visited[vertex] = true;
        
        boolean result = false;
        List<Integer> neighbors = adjList[vertex];
        
        if(neighbors!=null) {  // Important as we are constructing adjList only for vertices available in prerequisites array.
            for(Integer neighbor: neighbors) {

                if(visited[neighbor]) { // back-edge found!
                    result = true;
                    break;
                }

                if(discovered[neighbor]) { // If already discovered, no need to traverse again. Consider other neighbors!
                    continue;
                }

                if(findCycle(neighbor, adjList)) { // Perform DFS.
                    result = true;
                    break;
                }

            }
        }
        
        resultSet[counter++] = vertex; // Add the course to resultSet
        visited[vertex] = false;  // Very important line to find back-edge.
        return result;
        
    }
}

思路一代码是我自己写的,击败了2%的选手,难过

class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        //if(prerequisites == null) return null;
        int m = prerequisites.length;
        //if(m <= 0) return null;
        Map<Integer,Set<Integer>> in = new HashMap<Integer,Set<Integer>>();
        Map<Integer,Set<Integer>> out = new HashMap<Integer,Set<Integer>>();
        for(int i = 0 ; i < numCourses ; i++){
            in.put(i,new HashSet<Integer>());
            out.put(i,new HashSet<Integer>());
        }
        for(int i = 0 ; i < m ; i++){            
            in.get(prerequisites[i][0]).add(prerequisites[i][1]);
            out.get(prerequisites[i][1]).add(prerequisites[i][0]);
        }
        List<Integer> ans = new ArrayList<Integer>();
        Set<Integer> temp = new HashSet<Integer>();
        while(true){
            int start = -1;
            for(int i = 0 ; i < numCourses ; i++){
                if(in.get(i).size() == 0 && !temp.contains(i)){
                    start = i;
                    break;
                }
            }
            if(start == -1){
                if(ans.size() == numCourses) {
                    int[] res = new int[numCourses];
                    for(int i = 0 ; i < numCourses;i++){
                        res[i] = ans.get(i);
                    }
                    return res;
                }else{
                     return new int[0];
                }            
            }else{
                ans.add(start); 
                temp.add(start);
                for(int i : out.get(start)){
                    in.get(i).remove(start);
                }            
            }
        }
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
智慧校园整体解决方案是响应国家教育信息化政策,结合教育改革和技术创新的产物。该方案以物联网、大数据、人工智能和移动互联技术为基础,旨在打造一个安全、高效、互动且环保的教育环境。方案强调从数字化校园向智慧校园的转变,通过自动数据采集、智能分析和按需服务,实现校园业务的智能化管理。 方案的总体设计原则包括应用至上、分层设计和互联互通,确保系统能够满足不同用户角色的需求,并实现数据和资源的整合与共享。框架设计涵盖了校园安全、管理、教学、环境等多个方面,构建了一个全面的校园应用生态系统。这包括智慧安全系统、校园身份识别、智能排课及选课系统、智慧学习系统、精品录播教室方案等,以支持个性化学习和教学评估。 建设内容突出了智慧安全和智慧管理的重要性。智慧安全管理通过分布式录播系统和紧急预案一键启动功能,增强校园安全预警和事件响应能力。智慧管理系统则利用物联网技术,实现人员和设备的智能管理,提高校园运营效率。 智慧教学部分,方案提供了智慧学习系统和精品录播教室方案,支持专业级学习硬件和智能化网络管理,促进个性化学习和教学资源的高效利用。同时,教学质量评估中心和资源应用平台的建设,旨在提升教学评估的科学性和教育资源的共享性。 智慧环境建设则侧重于基于物联网的设备管理,通过智慧教室管理系统实现教室环境的智能控制和能效管理,打造绿色、节能的校园环境。电子班牌和校园信息发布系统的建设,将作为智慧校园的核心和入口,提供教务、一卡通、图书馆等系统的集成信息。 总体而言,智慧校园整体解决方案通过集成先进技术,不仅提升了校园的信息化水平,而且优化了教学和管理流程,为学生、教师和家长提供了更加便捷、个性化的教育体验。
Java中,ClassNotFoundException是一种异常类型,表示在运行时找不到指定的类。当使用Class.forName()方法或ClassLoader.loadClass()方法加载类时,如果找不到指定的类,就会抛出ClassNotFoundException异常。 对于你提到的ClassNotFoundException: CourseSchedule异常,它表示在运行时无法找到名为CourseSchedule的类。这可能是由于以下几个原因导致的: 1. 类名拼写错误:请确保你输入的类名正确无误。Java对类名是区分大小写的,所以请检查类名的大小写是否与实际类名一致。 2. 缺少依赖:如果CourseSchedule类依赖于其他类或库,而这些依赖项没有正确地被包含在项目中,就会导致ClassNotFoundException异常。请确保所有依赖项都已正确添加到项目中。 3. 类路径问题:如果CourseSchedule类所在的包或目录没有被正确地包含在类路径中,也会导致ClassNotFoundException异常。请检查类路径设置,确保包含了CourseSchedule类所在的路径。 解决这个问题的方法取决于你的具体情况。如果是第一种情况,你可以检查类名的拼写是否正确。如果是第二种情况,你需要确保所有依赖项都已正确添加到项目中。如果是第三种情况,你需要检查类路径设置是否正确。 如果你能提供更多的上下文信息,比如你是在什么情况下遇到这个异常,以及你的代码或项目结构,我可以给出更具体的解决方案。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值