Leetcode 997) Find the Town Judge

https://leetcode.com/problems/find-the-town-judge/

class Solution {
    public int findJudge(int n, int[][] trust) {
        boolean[] j = new boolean[n];//all false
        for(int i =0;i<trust.length;i++){
            j[trust[i][0]-1]=true;
        }
        int check=0;int judge=-1;
        for(int i =0;i<n;i++){
            if(!j[i]){
                check++;
                judge=i;
            }
        }
        if(check==1){
            for(int i =0;i<trust.length;i++){
                if(trust[i][1]-1==judge){
                    j[trust[i][0]-1]=false;
                }                         
            }
            for(int i =0;i<n;i++){
                if(j[i]){
                    return -1;
                }
            }
            return judge+1;
        }
        return -1;
    }
}

다른 답안

class Solution {
    public int findJudge(int n, int[][] trust) {
        int[] count=new int[n+1];
        for(int[] i : trust)
        {
            count[i[0]]--;
            count[i[1]]++;
        }
            for(int j=1 ; j<=n ;j++)
            {
                if(count[j]==n-1)
                    return j;
            }
        return -1;
    }
}

© 2018. All rights reserved.

Powered by Hydejack v8.5.2