Find the Town Judge
找到一个人, 被所有人trust, 但是不trust任何人. 依照题意写就行了.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
class Solution { public int findJudge(int N, int[][] trust) { int[] trusted = new int[1001]; // count of being trusted boolean[] trustTo = new boolean[1001]; // true = trust to someone for(int i = 0 ; i < trust.length; i++) { trusted[trust[i][1]]++; trustTo[trust[i][0]] = true; } for(int i = 1 ; i < 1001; i++) { if(trusted[i] == N-1 && !trustTo[i]) // N-1 = being trusted by all others expected self. trustTo = false -> trust noone return i; } return -1; } } |