自学内容网 自学内容网

Java LeetCode每日一题

        997. 找到小镇的法官

package JavaExercise20241002;

public class JavaExercise {
    public static void main(String[] args) {
        int[][] array = {{1,3},{2,3},{3,1}};
        Solution solution = new Solution();
        System.out.println(solution.findJudge(3, array));
    }
}

class Solution {
    public int findJudge(int n, int[][] trust) {
        int[] out = new int[n + 1];
        int[] in = new int[n + 1];

        for (int[] edges : trust) {
            int x = edges[0];
            int y = edges[1];
            out[x]++;
            in[y]++;
        }

        for (int i = 1; i <= n; i++) {
            if (out[i] == 0 && in[i] == (n - 1)) {
                return i;
            }
        }
        return -1;
    }
}


原文地址:https://blog.csdn.net/Aishangyuwen/article/details/142682620

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!