In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.
If the town judge exists, then:
The town judge trusts nobody. Everybody (except for the town judge) trusts the town judge. There is exactly one person that satisfies properties 1 and 2. You are given an array trust where trust[i] = [ai, bi] representing that the person labeled ai trusts the person labeled bi. If a trust relationship does not exist in trust array, then such a trust relationship does not exist.
Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.
Example 1:
1 2
Input: n = 2, trust = [[1,2]] Output: 2
Example 2:
1 2
Input: n = 3, trust = [[1,3],[2,3]] Output: 3
Example 3:
1 2
Input: n = 3, trust = [[1,3],[2,3],[3,1]] Output: -1
Constraints:
1 <= n <= 1000 0 <= trust.length <= 104 trust[i].length == 2 All the pairs of trust are unique. ai != bi 1 <= ai, bi <= n
題目要求我們要找出城鎮法官,而這座城鎮的人都相信法官,但法官不信任任何人 基於這個條件來找出法官 n 是總人數 trust[0] 相信 trust[1]
""" Wrong Answer """ classSolution: deffindJudge(self, n: int, trust: List[List[int]]) -> int: all_people = {i: Nonefor i inrange(1, n+1)} for path in trust: all_people.pop(path[1], None) for k in all_people.keys(): return k return -1
classSolution: deffindJudge(self, n: int, trust: List[List[int]]) -> int: all_people = {i: 0for i inrange(1, n+1)} for i in trust: all_people.pop(i[0], None) try: all_people[i[1]] += 1 except KeyError: pass for k, v in all_people.items(): if v == n - 1: return k return -1
funcfindJudge(n int, trust [][]int)int { TrustCounter := map[int]int{} result := -1 // Make map for i := 1; i <= n; i++{ TrustCounter[i] = 0 } for _, v := range trust { delete(TrustCounter, v[0]) if _, ok := TrustCounter[v[1]]; ok { TrustCounter[v[1]] += 1 } } for k, v := range TrustCounter{ if v == n - 1 { result = k } } return result }
classSolution { funcfindJudge(_n: Int, _trust: [[Int]]) -> Int { var trustCount: [Int: Int] = [:] for i in1...n { trustCount[i] =0 } for i in trust { trustCount.removeValue(forKey: i[0]) var beTrusted = trustCount[i[1]] if beTrusted !=nil { trustCount[i[1]] = beTrusted!+1 } } for (k, v) in trustCount{ if v == n -1 { return k } } return-1 } }
classSolution: deffindJudge(self, n: int, trust: List[List[int]]) -> int: trust_count = [0] * (n + 1) for a, b in trust: trust_count[a] -= 1 trust_count[b] += 1 for i inrange(1, n + 1): if trust_count[i] == n - 1: return i return -1
使用Golang
1 2 3 4 5 6 7 8 9 10 11 12 13
funcfindJudge(n int, trust [][]int)int { trustCount := make([]int, n+1) for _, t := range trust { trustCount[t[0]]-- trustCount[t[1]]++ } for i := 1; i <= N; i++ { if trustCount[i] == n - 1 { return i } } return-1 }
使用Swift
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
classSolution { funcfindJudge(_n: Int, _trust: [[Int]]) -> Int { var trustCount =Array(repeating: 0, count: n +1) for i in trust { trustCount[i[0]] -=1 trustCount[i[1]] +=1 } for i in1...n { if trustCount[i] == n -1 { return i } } return-1 } }