@@ -0,0 +1,2613 @@
+package leetcode;%0A%0Aimport java.util.ArrayList;%0Aimport java.util.HashMap;%0Aimport java.util.HashSet;%0Aimport java.util.List;%0Aimport java.util.Map;%0Aimport java.util.Set;%0A%0A/**%0A * https://leetcode.com/problems/course-schedule/%0A */%0Apublic class Problem207 %7B%0A    public boolean canFinish(int numCourses, int%5B%5D%5B%5D prerequisites) %7B%0A        Map%3CInteger, List%3CInteger%3E%3E graph = new HashMap%3C%3E();%0A        for (int i = 0; i %3C prerequisites.length; i++) %7B%0A            int%5B%5D array = prerequisites%5Bi%5D;%0A            int from = array%5B0%5D;%0A            int to = array%5B1%5D;%0A            if (graph.containsKey(from)) %7B%0A                graph.get(from).add(to);%0A            %7D else %7B%0A                List%3CInteger%3E edges = new ArrayList%3C%3E();%0A                edges.add(to);%0A                graph.put(from, edges);%0A            %7D%0A        %7D%0A        return !hasCycle(graph);%0A    %7D%0A%0A    private static class Cycle %7B%0A        boolean cycle;%0A    %7D%0A%0A    private boolean hasCycle(Map%3CInteger, List%3CInteger%3E%3E graph) %7B%0A        Map%3CInteger, Boolean%3E onStack = new HashMap%3C%3E();%0A        Set%3CInteger%3E marked = new HashSet%3C%3E();%0A        Cycle cycle = new Cycle();%0A        for (int vertex : graph.keySet()) %7B%0A            onStack.put(vertex, false);%0A        %7D%0A        for (int vertex : graph.keySet()) %7B%0A            if (!marked.contains(vertex)) %7B%0A                dfs(graph, vertex, onStack, marked, cycle);%0A                if (cycle.cycle) %7B%0A                    return true;%0A                %7D%0A            %7D%0A        %7D%0A        return false;%0A    %7D%0A%0A    private void dfs(Map%3CInteger, List%3CInteger%3E%3E graph, int source,%0A                        Map%3CInteger, Boolean%3E onStack, Set%3CInteger%3E marked,%0A                        Cycle cycle) %7B%0A        if (!graph.containsKey(source)) %7B%0A            return;%0A        %7D%0A        marked.add(source);%0A        onStack.put(source, true);%0A        for (int vertex : graph.get(source)) %7B%0A            if (!marked.contains(vertex)) %7B%0A                dfs(graph, vertex, onStack, marked, cycle);%0A            %7D else %7B%0A                if (onStack.get(vertex)) %7B%0A                    cycle.cycle = true;%0A                %7D%0A            %7D%0A        %7D%0A        onStack.put(source, false);%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem207 prob = new Problem207();%0A//        int%5B%5D%5B%5D adjList = new int%5B%5D%5B%5D%7B%0A//            new int%5B%5D%7B1, 0%7D,%0A//        %7D;%0A        int%5B%5D%5B%5D adjList = new int%5B%5D%5B%5D%7B%0A            new int%5B%5D%7B1, 0%7D,%0A            new int%5B%5D%7B0, 1%7D,%0A        %7D;%0A//        int%5B%5D%5B%5D adjList = new int%5B%5D%5B%5D%7B%0A//            new int%5B%5D%7B1, 0%7D,%0A//            new int%5B%5D%7B2, 0%7D,%0A//        %7D;%0A        System.out.println(prob.canFinish(3, adjList));%0A    %7D%0A%7D%0A

Update problem 207