0
点赞
收藏
分享

微信扫一扫

leetcode课程表

boom莎卡拉卡 2022-03-15 阅读 66

题目描述

在这里插入图片描述

分析

  • 本题是有向图的一道题目,题目要求判断是否可以完成所有课程的学习,所以考虑不能完成的情况是图中存在环。所以此题可以转换为判断给定的有向图是否有环
  • 由于本题给定的图的结构是n * 2 的二维数组,所以在这里我先将该图转换为我熟悉的结构。
  • 判断有向图是否有环,本文采用拓扑排序的方法,若最后的所有节点入度为0,则说明没有环。

code

class Solution {
    public class Edge {
        public int weight;
        public Node from;
        public Node to;

        public Edge(int weight, Node from, Node to) {
            this.weight = weight;
            this.from = from;
            this.to = to;
        }
    }
    public  class Node {
        public int value;       // 节点的值
        public int out;         // 节点出度
        public int in;          // 节点入度
        public List<Node> nexts;    // 该节点的相邻节点(有该节点引出的)
        public List<Edge> edges;    // 边(有该节点引出的边)
        public Node(int value) {
            this.value = value;
            this.out = 0;
            this.in = 0;
            this.nexts = new ArrayList<>();
            this.edges = new ArrayList<>();
        }
    }
    public class Graph {
        public HashMap<Integer, Node> nodes;        // 点集
        public HashSet<Edge> edges;                 // 边集
        public Graph() {
            nodes = new HashMap<>();
            edges = new HashSet<>();
        }
    }
    public Graph createGraph(int[][] prerequisites) {
        Graph graph = new Graph();
        int N = prerequisites.length;
        for (int i = 0; i < N; i++) {
            int from = prerequisites[i][0];
            int to = prerequisites[i][1];
            if (graph.nodes.get(from) == null) {
                graph.nodes.put(from, new Node(from));
            }
            if (graph.nodes.get(to) == null) {
                graph.nodes.put(to,new Node(to));
            }

            Node fromNode = graph.nodes.get(from);
            Node toNode = graph.nodes.get(to);
            Edge edge = new Edge(0, fromNode, toNode);
            fromNode.nexts.add(toNode);
            fromNode.out++;
            toNode.in++;
            fromNode.edges.add(edge);

            graph.edges.add(edge);

        }
        return graph;
    }

    public boolean canFinish(int numCourses, int[][] prerequisites) {
        if (numCourses > 0 && prerequisites.length == 0) {
            return true;
        }
        // 将给定的图结构转换为自己的图结构
        Graph graph = createGraph(prerequisites);
        // key  某一个node, value 该node剩余的入度, 这个存在时因为每次将入度为零的节点入队列均需要删除由其引出的百年的影响。
        Map<Node, Integer> inMap = new HashMap<>();
        Queue<Node> queue = new LinkedList<>();
        // 将入度为零的节点入队列
        for (Node node : graph.nodes.values()) {
            inMap.put(node,node.in);
            if (node.in == 0) {
                queue.add(node);
            }
        }

        while (!queue.isEmpty()) {
            Node curNode = queue.poll();
            
            // inMap中维护节点出队列以后的剩余入度,同时将入度为0的节点入队列
            for (Node next : curNode.nexts) {
                inMap.put(next, inMap.get(next) - 1);
                if (inMap.get(next) == 0) {
                    queue.add(next);
                }
            }
        }
        boolean res = true;
        for (Integer value : inMap.values()) {
            if (value > 0) {
                res = false;
                break;
            }
        }
        return res;
    }

}

总结

此题的测试用例真坑,本来想使用一个集合保存拓扑排序的结果,最后判断list.size() == numCourses 以决定是否有环,但是测试用例竟然出来numCourses > 给定的图中的节点数。最后采用了判断最后所有节点入度是否为0,来说明有没有环。

举报

相关推荐

0 条评论