# 997.Find the Town Judge

**997.Find the Town Judge**

难度:Easy

> 在一个小镇里，按从 1 到 N 标记了 N 个人。传言称，这些人中有一个是小镇上的秘密法官。

如果小镇的法官真的存在，那么：

小镇的法官不相信任何人。 每个人（除了小镇法官外）都信任小镇的法官。 只有一个人同时满足属性 1 和属性 2 。 给定数组 trust，该数组由信任对 trust\[i] = \[a, b] 组成，表示标记为 a 的人信任标记为 b 的人。

如果小镇存在秘密法官并且可以确定他的身份，请返回该法官的标记。否则，返回 -1。

```
示例 1：

输入：N = 2, trust = [[1,2]]
输出：2
示例 2：

输入：N = 3, trust = [[1,3],[2,3]]
输出：3
示例 3：

输入：N = 3, trust = [[1,3],[2,3],[3,1]]
输出：-1
示例 4：

输入：N = 3, trust = [[1,2],[2,3]]
输出：-1
示例 5：

输入：N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
输出：3
 

提示：

1 <= N <= 1000
trust.length <= 10000
trust[i] 是完全不同的
trust[i][0] != trust[i][1]
1 <= trust[i][0], trust[i][1] <= N
```

判断第二个数出现了N-1次，且该数不作为第一个参数。\
另外考虑特殊情况。

```
class Solution {
public:
    int findJudge(int N, vector<vector<int>>& trust) {
        if(N==1 && trust.empty() ) return 1;
        unordered_map<int, int> nums;
        unordered_set<int>num1;
        for(auto p:trust)
            num1.insert(p[0]);
        for(auto p: trust)
               if( ++nums[p[1]] ==N-1  && !num1.count(p[1]) )  return p[1];
        return -1;
    }
};
```

> 执行用时 :284 ms, 在所有 C++ 提交中击败了38.21%的用户\
> 内存消耗 :60.2 MB, 在所有 C++ 提交中击败了5.02%的用户


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://dfine.gitbook.io/leetcode/997.find_the_town_judge.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
