1.5
This commit is contained in:
1 parent
67f3e16535
commit
99ed42d095
8 files changed
+294
No files matched your search
@@ -0,0 +1,42 @@
|
||||
package com.hong.www;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class A210 {
|
||||
public int[] findOrder(int numCourses, int[][] prerequisites) {
|
||||
if(prerequisites.length==0){
|
||||
int[] temp = new int[numCourses];
|
||||
for(int i=0;i<numCourses;i++){
|
||||
temp[i]=i;
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
Arrays.sort(prerequisites,(a,b)->a[1]-b[1]);
|
||||
HashSet<Integer> temp =new HashSet<>();
|
||||
int[] fina =new int[numCourses];
|
||||
fina[0]=prerequisites[0][1];
|
||||
int n=1;
|
||||
temp.add(prerequisites[0][1]);
|
||||
for(int[] x:prerequisites){
|
||||
if(temp.contains(x[1])){
|
||||
if(temp.contains(x[0])){
|
||||
return new int[]{};
|
||||
}else if(!temp.contains(x[0])){
|
||||
temp.add(x[0]);
|
||||
fina[n]=x[0];
|
||||
n++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if(temp.size()==numCourses){
|
||||
return fina;
|
||||
}
|
||||
return new int[]{};
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
A210 a210 = new A210();
|
||||
int[][] prerequisites = {{1,0},{2,0},{3,1},{3,2}};
|
||||
int[] order = a210.findOrder(4, prerequisites);
|
||||
System.out.println(Arrays.toString(order));
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user