【leetcode】997. Find the Town Judge
题目如下:
In a town, there are
Npeople labelled from1toN. 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
trust, an array of pairstrust[i] = [a, b]representing that the person labelledatrusts the person labelledb.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: 2Example 2:
Input: N = 3, trust = [[1,3],[2,3]]
Output: 3Example 3:
Input: N = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1Example 4:
Input: N = 3, trust = [[1,2],[2,3]]
Output: -1Example 5:
Input: N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
Output: 3Note:
1 <= N <= 1000trust.length <= 10000trust[i]are all differenttrust[i][0] != trust[i][1]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的更多相关文章
- 【LeetCode】997. Find the Town Judge 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 度 日期 题目地址:https://leetcode ...
- 【Leetcode_easy】997. Find the Town Judge
problem 997. Find the Town Judge solution: class Solution { public: int findJudge(int N, vector<v ...
- 【LeetCode】657. Judge Route Circle 解题报告
[LeetCode]657. Judge Route Circle 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/problems/judge-route- ...
- 【leetcode】657. Robot Return to Origin
Algorithm [leetcode]657. Robot Return to Origin https://leetcode.com/problems/robot-return-to-origin ...
- 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java
[LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...
- 【Leetcode】Pascal's Triangle II
Given an index k, return the kth row of the Pascal's triangle. For example, given k = 3, Return [1,3 ...
- 53. Maximum Subarray【leetcode】
53. Maximum Subarray[leetcode] Find the contiguous subarray within an array (containing at least one ...
- 27. Remove Element【leetcode】
27. Remove Element[leetcode] Given an array and a value, remove all instances of that value in place ...
- 【刷题】【LeetCode】007-整数反转-easy
[刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...
随机推荐
- Joyoshare HEIC Converter for Mac将HEIC照片转换成其他格式的方法
如何把HEIC格式的照片转换成其JPEG,PNG,GIF他格式呢?使用Joyoshare HEIC Converter for Mac破解版就可以,Joyoshare HEIC Converter是可 ...
- 【leetcode】892. Surface Area of 3D Shapes
题目如下: 解题思路:对于v = grid[i][j],其表面积为s = 2 + v*4 .接下来只要在判断其相邻四个方向有没有放置立方体,有的话减去重合的面积即可. 代码如下: class Solu ...
- 多线程模拟生产者消费者示例之wait/notify
public class Test { public static void main(String[] args) throws InterruptedException { List<Str ...
- IDEA使用中的快捷键
项目与项目之间的跳转: Ctrl+Alt+] 下一个窗口. Ctrl+Alt+[ 跳转回上一个窗口. 文件之间的跳转: Ctrl+E. ...
- luogu P3768 简单的数学题 杜教筛 + 欧拉反演 + 逆元
求 $\sum_{i=1}^{n}\sum_{j=1}^{n}ijgcd(i,j)$ 考虑欧拉反演: $\sum_{d|n}\varphi(d)=n$ $\Rightarrow \sum_{i ...
- kNN(从文本文件中解析数据)
# 准备数据:从文本文件中解析数据# 在kNN.py中创建名为file2matrix的函数,处理输入格式问题# 该函数的输入为文件名字符串,输出为训练样本矩阵和类标签向量# 将文本记录到转换Numpy ...
- angular项目引用第三方公共js文件
由于项目需要,领导要求在正在开发的angular项目中,引入公共js,以便进行统计计算. 于是便各种找度娘,网上有好多引用jquery插件的例子,于是便按照步骤对自身项目进行了改造,先记录一下: st ...
- [HDU3117]Fibonacci Numbers
题目:Fibonacci Numbers 链接:http://acm.hdu.edu.cn/showproblem.php?pid=3117 分析: 1)后四位可以用矩阵快速幂解决.$T= \left ...
- QT中用QStettings生成INI文件来记录QFileDialog::getOpenFileName上次的打开路径
QSettings setting("./Setting.ini", QSettings::IniFormat); //QSettings能记录一些程序中的信息,下次再打开时可以读 ...
- 关于函数lower_bound()如何使用的问题
这个函数是c++ STL里自带的函数,应该需要引用头文件#include<iostream> 功能:在一个有序的序列中查找可以将value(一个变量)放在队列里面而不会引起序列长度变化,单 ...