1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
| class Solution { public int findJudge(int N, int[][] trust) { int n1 = trust.length; Map<Integer, List<Integer>> map = new HashMap<>(); for (int i = 0; i < n1; i++) { int[] arr = trust[i]; if (map.containsKey(arr[0])){ List<Integer> curList = map.get(arr[0]); curList.add(arr[1]); map.put(arr[0], curList); }else { List<Integer> list = new ArrayList<>(); list.add(arr[1]); map.put(arr[0], list); } }
if (map.size() != N - 1) { return -1; }
int k = -1; for (int i = 1; i <= N; i++) { if (!map.containsKey(i)) { k = i; break; } }
boolean b = true; for (int i = 1; i <= N; i++) { if (i == k) { continue; }
if (!map.get(i).contains(k)) { b = false; break; } }
return b ? k : -1; } }
|