leetcode课程表

题目描述

你这个学期必须选修 numCourses 门课程,记为 0 到 numCourses - 1 。
在选修某些课程之前需要一些先修课程。 先修课程按数组 prerequisites 给出,其中 prerequisites[i] = [ai, bi] ,表示如果要学习课程 ai 则 必须 先学习课程 bi 。

  • 例如,先修课程对 [0, 1] 表示:想要学习课程 0 ,你需要先完成课程 1 。
    请你判断是否可能完成所有课程的学习?如果可以,返回 true ;否则,返回 false 。

在这里插入图片描述

分析

  • 本题是有向图的一道题目,题目要求判断是否可以完成所有课程的学习,所以考虑不能完成的情况是图中存在环。所以此题可以转换为判断给定的有向图是否有环
  • 由于本题给定的图的结构是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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值