#Leetcode# 997. Find the Town Judge
https://leetcode.com/problems/find-the-town-judge/
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:
- 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 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 <= N <= 1000
trust.length <= 10000
trust[i]
are all differenttrust[i][0] != trust[i][1]
1 <= trust[i][0], trust[i][1] <= N
代码:
class Solution {
public:
vector<int> v[1010];
int vis[1010];
int findJudge(int N, vector<vector<int>>& trust) {
int n = trust.size(); for(int i = 0; i < n; i ++) {
int a = trust[i][0], b = trust[i][1];
v[b].push_back(a);
vis[a] = 1;
} int cnt = 0, temp = 0;
for(int i = 1; i <= N; i ++) {
if(vis[i] == 0 && v[i].size() == N - 1) {
cnt ++;
temp = i;
}
}
if(cnt == 1) return temp;
return -1;
}
};
#Leetcode# 997. Find the Town Judge的更多相关文章
- 【Leetcode_easy】997. Find the Town Judge
problem 997. Find the Town Judge solution: class Solution { public: int findJudge(int N, vector<v ...
- 【LeetCode】997. Find the Town Judge 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 度 日期 题目地址:https://leetcode ...
- 【leetcode】997. Find the Town Judge
题目如下: In a town, there are N people labelled from 1 to N. There is a rumor that one of these people ...
- [Swift]LeetCode997. 找到小镇的法官 | Find the Town Judge
In a town, there are N people labelled from 1 to N. There is a rumor that one of these people is se ...
- LeetCode.997-找到镇法官(Find the Town Judge)
这是悦乐书的第373次更新,第400篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第234题(顺位题号是997).在一个城镇,有N个人从1到N标记.有传言说其中一个人是秘 ...
- LeetCode题解之 Find the Town Judge
1.题目描述 2.问题分析 使用map set数据结构. 3.代码 int findJudge(int N, vector<vector<int>>& trust) { ...
- LeetCode997. Find the Town Judge
题目 在一个小镇里,按从 1 到 N 标记了 N 个人.传言称,这些人中有一个是小镇上的秘密法官. 如果小镇的法官真的存在,那么: 小镇的法官不相信任何人. 每个人(除了小镇法官外)都信任小镇的法官. ...
- 算法与数据结构基础 - 图(Graph)
图基础 图(Graph)应用广泛,程序中可用邻接表和邻接矩阵表示图.依据不同维度,图可以分为有向图/无向图.有权图/无权图.连通图/非连通图.循环图/非循环图,有向图中的顶点具有入度/出度的概念. 面 ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- Request.Params
在开发中有时会用到Request.Params["id"]来获取参数,那么到底是从什么地方接收参数呢? 一般情况下,有三种方式进行参数传递1.GET 方式,通过url传递,如?id ...
- Go中变量作用域的小坑
直接上一实例: package main import "fmt" func main(){ fmt.Println("for start") ; a < ...
- 学JAVA的第二天,静态网站制作,脑阔一点疼
先从下载apache-tomcat-9.0.17开始 在下边这个网站下载,下边一步步来 下面删除的这些是暂时用不上的,先吧它删除了,因为会拖慢启动速度 下边把ROOT里边除WEB-INF外的全不删除了 ...
- Win10操作系统下Oracle VM VirtualBox6.0加载磁盘提示“发现无效设置”的解决方法(包括“不能桥接网卡”问题的解决方法)
自从电脑换成Win10操作系统后,想借助VMBox处理一些事情,但是遇到了不能桥接网卡的问题,还以为是程序坏了,于是卸载重新安装,但是不行.接着又尝试在虚拟机里重新安装操作系统,还是不行. 然后百度了 ...
- C# 正则表达式应用
正则表达式平时不常用,经常都是用的时候,临时抱佛脚,查文档,然后就是被各种坑之后,才会逐渐熟练. 在线正则表达式测试:http://tool.oschina.net/regex/ 在线JSON格式化: ...
- 总结:BGP和静态路由并存,达到故障自动倒换的目的。
总体结论: 在上云的场景中,客户需要本地数据中心到云上VPC,出现网络故障时做到自动倒换,保证业务不中断. 一.客户需求 1.客户有总厂.分厂.总厂是通过专线和VPN连接上云,分厂是通过专线先连接到总 ...
- 随意下载:afinal jar
刚才用到afinal,下载jar包到处要积分啥的,我花钱下载了,现在免费给兄弟们 链接: https://pan.baidu.com/s/1BmB-06ui1D2BfooUCi6xKg 提取码: m3 ...
- QT获取本机IP和Mac地址
#include <QNetworkInterface> #include <QList> void MainWindow::getIPPath() { QString str ...
- ORA-02030: can only select from fixed tables/views
有时候给一些普通用户授予查询系统对象(例如dynamic performance views)权限时会遇到"ORA-02030: can only select from fixed tab ...
- User Agent 大全
一.基础知识篇: Http Header之User-Agent User Agent中文名为用户代理,是Http协议中的一部分,属于头域的组成部分,User Agent也简称UA.它是一个特殊字符串头 ...