classSolution(object):deffindJudge(self, N, trust):"""
:type N: int
:type trust: List[List[int]]
:rtype: int
"""if N==1:return1
cnt=collections.Counter()for t in trust:
cnt[t[1]]+=1
sign=False
result=Nonefor k in cnt:if cnt[k]==N-1:if sign==True:return-1else:
sign=True
result=k
if result:for t in trust:if t[0]==result:return-1return result
return-1
优秀代码
classSolution(object):deffindJudge(self, N, trust):"""
:type N: int
:type trust: List[List[int]]
:rtype: int
"""'''
[1, ...., N]
A trusts B
C trusts B
B trustees = [A, C]
'''
x =set(range(1, N+1))for(a, b)in trust:if a in x:
x.remove(a)iflen(x)!=1:return-1
judge_cand =list(x)[0]
cnt_trust =0for(a, b)in trust:if b == judge_cand:
cnt_trust +=1if cnt_trust == N-1:return judge_cand
return-1