---
# System prepended metadata

title: 997. Find the Town Judge
tags: [Leetcode]

---

<style>
    html, body, .ui-content {
    background: #222222;
    color: #00BFFF;
}
    /* 設定 code 模板 */
    .markdown-body code,
.markdown-body tt {
    background-color: #ffffff36;
}
.markdown-body .highlight pre,
.markdown-body pre {
    color: #ddd;
    background-color: #00000036;
}
.hljs-tag {
    color: #ddd;
}
.token.operator {
  background-color: transparent;
}    
/* 設定連結 */
a,
.open-files-container li.selected a {
    color: #89FFF8;
    
}
a:hover,
.open-files-container li.selected a:hover {
    color: #89FFF890;
}
   
</style>

###### tags: `Leetcode`

# 997. Find the Town Judge

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

## 題目
回傳town judge的位置
town judge:
1. The town judge trusts nobody.
2. Everybody (except for the town judge) trusts the town judge.
3. There is exactly one person that satisfies properties 1 and 2.
## 程式碼 I
```cpp=
class Solution {
public:
    int findJudge(int n, vector<vector<int>>& trust) {
        const int size = trust.size();
        vector<int> trusted(n, 0);
        for(int i = 0;i < size;i++){
            trusted[trust[i][1] - 1]++;
            trusted[trust[i][0] - 1]--;
        }
        for(int i = 0;i < n;i++)
            if(trusted[i] == n - 1)
                return i + 1;
        return -1;
    }
};
```
## Date 
### 2023/1/23