#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 <= 1000trust.length <= 10000trust[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 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- 第26章 联合注销 - Identity Server 4 中文文档(v1.0.0)
联合注销是指用户使用外部身份提供程序登录IdentityServer,然后用户通过IdentityServer未知的工作流程注销该外部身份提供程序的情况.当用户注销时,对IdentityServer进 ...
- Mybatis框架基础支持层——反射工具箱之MetaClass(7)
简介:MetaClass是Mybatis对类级别的元信息的封装和处理,通过与属性工具类的结合, 实现了对复杂表达式的解析,实现了获取指定描述信息的功能 public class MetaClass { ...
- REST风格下如何放行静态资源
在配置DispatcherServlet(前端控制器)时,如果把拦截路径配置成rest风格(即斜杠/),则会将静态资源也一并拦截(比如.css .js ,jpg)为了避免这个情况,可以把拦截路径设置成 ...
- 洛谷P5108 仰望半月的夜空(后缀数组)
题意 题目链接 Sol warning:下面这个做法只有95分,本地拍了1w+组都没找到错误我表示十分无能为力 我们考虑每个串的排名去更新答案,显然排名为\(1\)的后缀的前缀一定是当前长度的字典序最 ...
- 0.react学习笔记-环境搭建与脚手架
0.环境搭建 笔者使用的是deepin/mac两种系统,因为两个电脑经常切换用.环境搭建没什么区别. 0.1 node安装 按照node官网叙述安装 # Using Debian, as root c ...
- 案例解析|政府信息化的BI建设应用 .
一.行业背景 某建设厅综合监管信息化平台,是政企业务协同的平台之一,同时兼具协作.门户.办公应用集成.用户权限管理等多项功能.在此要求基础上,选择中间件基础技术平台,可以在最大程度满足平台功能需求的前 ...
- iOS-----------计算两个时间的时间差
UIButton * nameButton = [UIButton buttonWithType:UIButtonTypeCustom]; nameButton.frame = CGRectMake( ...
- 数据库CRUD操作以及MyBatis的配置使用
• 业务字段设计 • 数据库创建 • CRUD操作 • MyBatis集成 • 注解和XML定义 • ViewObject和DateTool • 首页开发 • 业务字段设计 实体: name: ...
- Spring Boot 相关
SpringBoot工程 参数解析 HTTP Method Request / Response / Session Error/重定向 Logger IoC AOP/Aspect 1:Sprin ...
- matlab练习程序(对应点集配准的四元数法)
这个算是ICP算法中的一个关键步骤,单独拿出来看一下. 算法流程如下: 1.首先得到同名点集P和X. 2.计算P和X的均值up和ux. 3.由P和X构造协方差矩阵sigma. 4.由协方差矩阵sigm ...