public class Solution { /** * @param numCourses a total of n courses * @param prerequisites a list of prerequisite pairs * @return the course order */ public int[] findOrder(int numCourses, int[][] prerequisites) { // Write your code here if (numCourses == 0) { return new int[0]; } int[] indegree = getIndegree(numCourses, prerequisites); Queue<Integer> queue = new LinkedList<>(); int count = 0; List<Integer> order = new ArrayList<>(); int[] result = new int[numCourses]; for (int i = 0; i < numCourses; i++) { if (indegree[i] == 0) { result[count++] = i; queue.add(i); // order.add(i); } } /*if (order.size() == numCourses) { return result; }*/ if (count == numCourses) { return result; } while (!queue.isEmpty()) { int node = queue.poll(); for (int i = 0; i < prerequisites.length; i++) { if (indegree[prerequisites[i][1]] == node) { indegree[prerequisites[i][0]]-=1; if (indegree[prerequisites[i][0]] == 0) { queue.add(prerequisites[i][0]); result[count++] = prerequisites[i][0]; // order.add(prerequisites[i][0]); } } } } if (count == numCourses) { return result; } /*if (order.size() == numCourses) { return result; }*/ else { return new int[0]; } } private int[] getIndegree(int numCourses, int[][] prerequisites) { int[] indegree = new int[numCourses]; for (int i = 0; i < numCourses; i++) { indegree[i] = 0; } if (prerequisites == null || prerequisites.length == 0 || prerequisites[0].length == 0) { return indegree; } for (int j =0; j < prerequisites.length; j++) { for (int k = 0; k < j; k++) { boolean repeat = false; if (prerequisites[k][0] == prerequisites[k][0] && prerequisites[j][0] == prerequisites[j][1]){ repeat = true; } if (repeat == false) { indegree[prerequisites[j][0]] +=1; } } } return indegree; } }
第四次写,放弃了。
public class Solution { /** * @param numCourses a total of n courses * @param prerequisites a list of prerequisite pairs * @return the course order */ public int[] findOrder(int numCourses, int[][] prerequisites) { // Write your code here if (numCourses == 0) { //prerequisites == null || prerequisites.length == 0 || prerequisites[0].length ==0 return new int[0]; } ArrayList<Integer> order = new ArrayList<>(); Map<Integer, Integer> indegree = getIndegree(numCourses, prerequisites); Queue<Integer> queue = new LinkedList<>(); for (int i = 0; i < numCourses; i++) { if (indegree.get(i) == 0) { queue.add(i); order.add(i); } } if (order.size() == numCourses) { //强制转换里是int[],不要忘了[] //int[] result = (int[]) order.toArray(); int[] result = new int[numCourses]; //order.toArray(result); for (int i = 0 ; i < numCourses; i++) { result[i] = (int)order.get(i); } return result; } while (!queue.isEmpty()) { int node = queue.poll(); for (int i = 0; i < prerequisites.length; i++) { if (prerequisites[i][1] == node) { indegree.put(prerequisites[i][0], indegree.get(prerequisites[i][0]) - 1) ; if (indegree.get(prerequisites[i][0]) == 0) { queue.add(prerequisites[i][0]); order.add(prerequisites[i][0]); } } } } if (order.size() == numCourses) { //强制转换里是int[],不要忘了[] //int[] result = (int[]) order.toArray(); int[] result = new int[numCourses]; //order.toArray(result); for (int i = 0 ; i < numCourses; i++) { result[i] = (int)order.get(i); } return result; } return new int[0]; } private Map<Integer, Integer> getIndegree(int numCourses, int[][] prerequisites) { Map<Integer, Integer> indegree = new HashMap(); //初始化 for (int i = 0; i < numCourses; i++) { indegree.put(i, 0); } //统计依赖个数 if (prerequisites == null || prerequisites.length == 0 || prerequisites[0].length == 0) { return indegree; } else { for (int j = 0; j < prerequisites.length; j++) { indegree.put(prerequisites[j][0], indegree.get(prerequisites[j][0]) + 1); } return indegree; } } }
这个版本写的时间超了,看答案好像用数组就可以统计,竟然用map。。。
public class Solution { /** * @param numCourses a total of n courses * @param prerequisites a list of prerequisite pairs * @return the course order */ private class Node { int label; ArrayList<Integer> pre; /* public node(int la; ArrayList<Integer> prerequist) { label = la; pre = pre; } */ } public int[] findOrder(int numCourses, int[][] prerequisites) { // Write your code here if (prerequisites == null || prerequisites.length == 0 || prerequisites[0].length == 0) { return new int[0]; } int[] result = new int[numCourses]; ArrayList<int> order = new ArrayList<int>(); Map<int node, ArrayList<int> request> indegree = getIndegree(numCourses,prerequisites); Queue<int> queue = new LinkedList<>(); for (int i = 0; i < numCourses; i++) { if (indegree.get(i) == 0) { queue.offer(i); order.add(i); } } while (!queue.isEmpty()) { int node = queue.poll; for (int i = 0; i < prerequisites.length; i++) { if(prerequisites[i][1] = node) { prerequisites[i][0] } } } } private Map<int node, ArrayList<int> request> getIndegree (int numCourses, int[][] prerequisites) { Map<int node, ArrayList<int> request> indegree = new HashMap(); //init map for (int j = 0; j < numCourses; j++) { indegree.put(j, new ArrayLisr<int>()); } for (int i = 0; i < prerequisites.length; i++) { indegree.put(prerequisites[i][0], indegree.get(prerequisites[i][0]).add()); } } }
写不出来