--- title: 997. Find the Town Judge tags: graph description: share source code. --- # 997. Find the Town Judge ```java= class Solution { public int findJudge(int n, int[][] trust) { List<Integer>[] graph = new ArrayList[n + 1]; Arrays.setAll(graph, v -> new ArrayList<>() ); for(int t [] : trust){ graph[ t[0]].add( t[1]); } int judge = -1; int cnt = 0; // 先找到 judge for(int i = 1; i <= n; i ++){ if(graph[i].size() == 0){ judge = i; // 超過ㄧ個以上的 judge if(++cnt > 1){ return -1; }; } } //沒找到 judge if(judge == -1){ return -1; } //有人不相信 judge => 可能不同獨立的 graph for(int i = 1; i <= n; i ++){ if(judge == i){ continue; } if(!graph[i].contains(judge)){ return -1; } } return judge; } } ```