zoukankan      html  css  js  c++  java
  • 课程安排

        //方法一  入度表(广度优先遍历)
        public boolean canFinish(int numCourses,int[][] prerequisites){
            int[] indegrees = new int[numCourses];
            List<List<Integer>> adjacency = new ArrayList<List<Integer>>();
            Queue<Integer> queue = new LinkedList<>();
            for(int i=0;i<numCourses;i++){
                adjacency.add(new ArrayList<Integer>());
            }
            for(int[] cp:prerequisites){
                indegrees[cp[0]]++;
                adjacency.get(cp[1]).add(cp[0]);
            }
            for(int i=0;i<numCourses;i++){
                if(indegrees[i]==0){
                    queue.add(i);
                }
            }
            while(!queue.isEmpty()){
                int pre = queue.poll();
                numCourses--;
                for(int cur:adjacency.get(pre)){
                    if(--indegrees[cur]==0){
                        queue.add(cur);
                    }
                }
            }
            return numCourses == 0;
        }
    // 方法二  深度优先遍历
        public boolean canFinish2(int numCourses,int[][] prerequisites){
            List<List<Integer>> adjacency = new ArrayList<List<Integer>>();
            for(int i=0;i<numCourses;i++){
                adjacency.add(new ArrayList<Integer>());
            }
            for(int[] cp:prerequisites){
                adjacency.get(cp[1]).add(cp[0]);
            }
            int[] flags = new int[numCourses];
            for(int i=0;i<numCourses;i++){
                if(!dfs(adjacency,flags,i)){
                    return false;
                }
            }
            return true;
        }
        private boolean dfs(List<List<Integer>> adjacency,int[] flags,int i){
            if(flags[i] == 1){
                return false;
            }
            if(flags[i]== -1){
                return true;
            }
            flags[i] = 1;
            for(Integer j:adjacency.get(i)){
                if(!dfs(adjacency, flags, j)){
                    return false;
                }
            }
            flags[i] = -1;
            return true;
        }
  • 相关阅读:
    方法是Objective-C独有的一种结构,只能在Objective-C中声明、定义和使用,C语言不能声明、定义和使用
    NSDate
    runtime
    iOS开发常用的工具
    程序的国际化
    经常使用的iOS SDK库和第三方库
    RunLoop是什么?
    狼若回头,必有理由
    第1年1月21日 Guard Malloc
    第1年1月10日 flv格式
  • 原文地址:https://www.cnblogs.com/helloworldmybokeyuan/p/13446369.html
Copyright © 2011-2022 走看看