# Leetcode 997. Find the Town Judge ###### tags: `Leetcode(C++)` 題目 : https://leetcode.com/problems/find-the-town-judge/submissions/ 。 想法 : 按照題目敘述去找Town Judge,沒有就輸出-1。 時間複雜度 : O(n)。 程式碼 : ``` class Solution { public: int findJudge(int n, vector<vector<int>>& trust) { int l = trust.size(), label[1010] = {0}, label2[1010] = {0}, townJ = 0, ok = 1; for(int i=0 ; i<l ; i++){ label[trust[i][0]] = 1; } for(int i=1 ; i<=n ; i++){ if(label[i] == 0){ townJ = i; } } if(townJ == 0) return -1; for(int i=0 ; i<l ; i++){ if(trust[i][1] == townJ){ label2[trust[i][0]] = 1; } } for(int i=1 ; i<=n ; i++){ if(label2[i] == 0 && i != townJ){ return -1; } } return townJ; } }; ```