leetcode997

 1 class Solution:
 2     def findJudge(self, N: int, trust: 'List[List[int]]') -> int:
 3         if N==1 and len(trust)==0:
 4             return 1
 5         s = list(range(1,N+1))
 6         d = {}
 7         for t in range(len(trust)):
 8             a1 = trust[t][0]
 9             a2 = trust[t][1]
10             if a1 in s:
11                 s.remove(a1)
12             if a2 in d.keys():
13                 d.update({a2:d[a2]+1})
14             else:
15                 d.update({a2:1})
16         if len(s)==1 and s[0] in d.keys() and d[s[0]]==N-1:
17             return s[0]
18         else:
19             return -1

猜你喜欢

转载自www.cnblogs.com/asenyang/p/10425841.html