当前位置: 代码迷 >> 综合 >> Leetcode 997. Find the Town Judge
  详细解决方案

Leetcode 997. Find the Town Judge

热度:20   发布时间:2023-12-12 21:12:14.0

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Find the Town Judge

2. Solution

  • Version 1
class Solution:def findJudge(self, N, trust):if N == 1:return 1if len(trust) < N - 1:return -1judge = {
    }people = {
    }for pair in trust:people[pair[0]] = people.get(pair[0], 0) + 1judge[pair[1]] = judge.get(pair[1], 0) + 1for key, value in judge.items():if value == N - 1 and key not in people:return keyreturn -1
  • Version 2
class Solution:def findJudge(self, N, trust):count = [0] * (N + 1)for pair in trust:count[pair[0]] -= 1count[pair[1]] += 1for i in range(1, len(count)):if count[i] == N - 1:return ireturn -1

Reference

  1. https://leetcode.com/problems/find-the-town-judge/submissions/
  相关解决方案