997. 找到小镇的法官
程序员文章站
2022-05-21 15:05:58
...
解题思路:
入度是N-1,出度是0,
代码:
public static void main(String[] args) {
int N=3;
int[][] trust={{1,3},{2,3}};
int judge = findJudge(N, trust);
System.out.println(judge);
}
public static int findJudge(int N, int[][] trust) {
int[] in=new int[N+1];
int[] out=new int[N+1];
for(int i=0;i<trust.length;i++){
out[trust[i][0]]++;
in[trust[i][1]]++;
}
for(int i=1;i<N+1;i++){
if(in[i]==(N-1) && out[i]==0){
return i;
}
}
return -1;
}