欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

997 找到小镇法官

程序员文章站 2022-05-21 15:06:52
...

目标点入度 == N - 1,出度 = 0

class Solution {
public:
    int findJudge(int N, vector<vector<int>>& trust) {
        int indegree[N + 1], outdegree[N + 1];
        memset(indegree, 0, sizeof(indegree));
        memset(outdegree, 0, sizeof(outdegree));
        for(auto i : trust)
            ++outdegree[i[0]], ++indegree[i[1]];
        for(int i = 1; i <= N; ++i)
            if(indegree[i] == N - 1 && !outdegree[i])
                return i;
        return -1;
    }
};
相关标签: Leetcode