leetcode 997. Find the Town Judge

记录每个人的信任数和被信任数,满足要求的就是目标值

class Solution {
    public int findJudge(int N, int[][] trust) {
        int[][] res = new int[N+1][2];
        for(int i=0;i<trust.length;i++){
            res[trust[i][0]][0]++;
            res[trust[i][1]][1]++;
        }
        for(int i=1;i<res.length;i++){
            if(res[i][0]==0 && res[i][1]==N-1){
                return i;
            }
        }
        return -1;
    }
}

你可能感兴趣的:(leetcode)