题目如下:

In a town, there are N people labelled from 1 to N.  There is a rumor that one of these people is secretly the town judge.

If the town judge exists, then:

  1. The town judge trusts nobody.
  2. Everybody (except for the town judge) trusts the town judge.
  3. There is exactly one person that satisfies properties 1 and 2.

You are given trust, an array of pairs trust[i] = [a, b] representing that the person labelled a trusts the person labelled b.

If the town judge exists and can be identified, return the label of the town judge.  Otherwise, return -1.

Example 1:

Input: N = 2, trust = [[1,2]]
Output: 2

Example 2:

Input: N = 3, trust = [[1,3],[2,3]]
Output: 3

Example 3:

Input: N = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1

Example 4:

Input: N = 3, trust = [[1,2],[2,3]]
Output: -1

Example 5:

Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
Output: 3

Note:

  1. 1 <= N <= 1000
  2. trust.length <= 10000
  3. trust[i] are all different
  4. trust[i][0] != trust[i][1]
  5. 1 <= trust[i][0], trust[i][1] <= N

解题思路:创建一个长度等于N的数组stat,记stat[i]为第i+1个人被别人信任的次数与信任别人的次数的差值,如果恰好等于N-1,则表明这个人就是法官。

代码如下:

class Solution(object):
def findJudge(self, N, trust):
"""
:type N: int
:type trust: List[List[int]]
:rtype: int
"""
stat = [0] * N
for x,y in trust:
stat[x-1] -= 1
stat[y-1] += 1
for i,v in enumerate(stat):
if v == N-1:
return i+1
return -1

【leetcode】997. Find the Town Judge的更多相关文章

  1. 【LeetCode】997. Find the Town Judge 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 度 日期 题目地址:https://leetcode ...

  2. 【Leetcode_easy】997. Find the Town Judge

    problem 997. Find the Town Judge solution: class Solution { public: int findJudge(int N, vector<v ...

  3. 【LeetCode】657. Judge Route Circle 解题报告

    [LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...

  4. 【leetcode】657. Robot Return to Origin

    Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...

  5. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  6. 【Leetcode】Pascal&#39;s Triangle II

    Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...

  7. 53. Maximum Subarray【leetcode】

    53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...

  8. 27. Remove Element【leetcode】

    27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...

  9. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

随机推荐

  1. 一张图理解"Figure", "Axes", "Axis"

    Figure is the object with the highest level in the hierarchy. It corresponds to the entire graphical ...

  2. find命令查找目录

    find <path> -type d -name "dir_name" -type d是查找目录的参数,如果是查找其他的: -type b: Block specia ...

  3. Linux进程前后台管理(&,fg, bg)

    将进程置于后台 xlogo & 会把进程置于后台管理,使用ps命令查看进程 PID. 使用命令jobs [1]+ Running xlogo & 可以看到正在运行的 xlogo 进程. ...

  4. threeJS射线拾取机制及案例

    前言 在浏览器中浏览三维图形的时候,有时想要与三维图形之间做一些点击事件和交互操作,其中比较常用的一个解决方案就是使用Raycaster对象来实现(射线拾取). 基础知识 世界坐标系:webGL中,世 ...

  5. MyEclipse使用过程中的问题及对应设置的总结

    1.关闭 Javaweb项目中的updating index : Window => Preferences => Myeclipse Enterprise Workbench => ...

  6. php strtok()函数 语法

    php strtok()函数 语法 作用:逐一分割字符串大理石构件 语法:strtok(string,split) 参数: 参数 描述 string 必需.规定要分割的字符串. split 必需.规定 ...

  7. HDU 6055 Regular polygon —— 2017 Multi-University Training 2

    Regular polygon Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)T ...

  8. python装饰器参数那些事_接受参数的装饰器

    # -*- coding: utf-8 -*- #coding=utf-8 ''' @author: tomcat @license: (C) Copyright 2017-2019, Persona ...

  9. HDU-4475 Downward paths(找规律)

    Downward paths Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  10. Python 进阶_迭代器 & 列表解析

    目录 目录 迭代器 iter 内建的迭代器生成函数 迭代器在 for 循环中 迭代器与字典 迭代器与文件 创建迭代器对象 创建迭代对象并实现委托迭代 迭代器的多次迭代 列表解析 列表解析的样例 列表解 ...