LeetCode997. Find the Town Judge
题目
在一个小镇里,按从
1到N标记了N个人。传言称,这些人中有一个是小镇上的秘密法官。如果小镇的法官真的存在,那么:
- 小镇的法官不相信任何人。
- 每个人(除了小镇法官外)都信任小镇的法官。
- 只有一个人同时满足属性 1 和属性 2 。
给定数组
trust,该数组由信任对trust[i] = [a, b]组成,表示标记为a的人信任标记为b的人。如果小镇存在秘密法官并且可以确定他的身份,请返回该法官的标记。否则,返回
-1。示例 1:
输入:N = 2, trust = [[1,2]]
输出:2
示例 2:
输入:N = 3, trust = [[1,3],[2,3]]
输出:3
示例 3:
输入:N = 3, trust = [[1,3],[2,3],[3,1]]
输出:-1
示例 4:
输入:N = 3, trust = [[1,2],[2,3]]
输出:-1
示例 5:
输入:N = 4, trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]
输出:3提示:
1 <= N <= 1000trust.length <= 10000trust[i]是完全不同的trust[i][0] != trust[i][1]1 <= trust[i][0], trust[i][1] <= N
法1。有向图。把二维数组看作图。对是有向边。 出度为0,入度 为N-1的就是法官。Time O(T + N), space O(N)
class Solution {
public:
int findJudge(int N, vector<vector<int>>& trust) {
vector<int> count(N + 1, 0);
for (auto& t : trust)
count[t[0]]--, count[t[1]]++;
for (int i = 1; i <= N; ++i) {
if (count[i] == N - 1) return i;
}
return -1;
}
};
法2。开一个N+1长度的容器。容器的索引是人的序号。把对放入容器里。pair<出度,入度>。遍历数组,更改每个人的出度和入度。最后判断。出度为0,入度 为N-1的就是法官。
int findJudge(int N, vector<vector<int>>& trust) {
vector<pair<int, int>> cn(N + 1);
for (auto &t : trust) ++cn[t[0]].first, ++cn[t[1]].second;
for (auto i = 1; i <= N; ++i) if (cn[i].first == 0 && cn[i].second == N - 1) return i;
return -1;
}
LeetCode997. Find the Town Judge的更多相关文章
- [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
https://leetcode.com/problems/find-the-town-judge/ In a town, there are N people labelled from 1 to ...
- 【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
题目如下: In a town, there are N people labelled from 1 to N. There is a rumor that one of these people ...
- 【LeetCode】997. Find the Town Judge 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 度 日期 题目地址:https://leetcode ...
- LeetCode题解之 Find the Town Judge
1.题目描述 2.问题分析 使用map set数据结构. 3.代码 int findJudge(int N, vector<vector<int>>& trust) { ...
- LeetCode.997-找到镇法官(Find the Town Judge)
这是悦乐书的第373次更新,第400篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第234题(顺位题号是997).在一个城镇,有N个人从1到N标记.有传言说其中一个人是秘 ...
- [LeetCode] Find the Celebrity 寻找名人
Suppose you are at a party with n people (labeled from 0 to n - 1) and among them, there may exist o ...
- LeetCode Find the Celebrity
原题链接在这里:https://leetcode.com/problems/find-the-celebrity/ 题目: Suppose you are at a party with n peop ...
随机推荐
- 使用Xshell连接服务器
转载原地址:http://www.server110.com/linux/201308/830.html 1)关于Xshell 网上更多的资料里提到的SSH客户端是putty,因为简单.开源.免费.但 ...
- Spark集群安装MySQL环境
1.修改yum源 鉴于用国外的Yum源,速度比较慢,所以想到将国外的yum源改为国内的Yum源,这里选择使用比较多的阿里云源.具体修改方法可以参考此连接 我们先新建一个新的文件用来存放Yum源: [k ...
- stm32 PWM输出学习
STM32 的定时器除了 TIM6 和 7,其他的定时器都可以用来产生 PWM 输出.其中高级定时器 TIM1 和 TIM8 可以同时产生多达 7 路的 PWM 输出.通用定时器也能同时产生多达 4路 ...
- anaular js loadding效果
以前用的jquery的时候,用ajax实现,比较好弄,下面是angularjs的方式: //body下面增加div <div data-loading></div> //dir ...
- IIS报错试图加载格式不正确的程序集解决办法 64位WinServer系统兼容32位程序集
问题描述: 将网站/WCF发布到IIS后运行报这个错. 原因: 64位系统和32系统不匹配导致. 解决办法: 方法一.注册32位IIS(必须卸载64位IIS) ...
- mysql关键字执行顺序
from on join where group by having select distinct union order by 昨天去58面试,之前的java基础和数据结构算法之类的都还可以,最后 ...
- The fifth day
All men cannot be first . 今日单词: first(形容词):第一的:基本的:最早的:(副词):第一:首先 翻译:不可能人人都是第一名. <Only Love>-- ...
- 内表转WORD
组合HTML字符串的方法来导出WORD文件 DATA: BEGIN OF wa_html, zhtml(), END OF wa_html, gt_html LIKE TABLE OF wa_html ...
- OSI七层模型含义
应用层:由用户自己规定,只要形成的消息能与表示层接口.这包括各机互访协议,分布式数据库协议等. 表示层:是在满足用户需求的基础上,尽可能的节省传输费用而设置的.如文本压缩.常用词转换.加密.变更文件格 ...
- 【起航计划 024】2015 起航计划 Android APIDemo的魔鬼步伐 23 App->Notification->IncomingMessage 状态栏通知
应用程序可以使用Notifications来通知用户某个事件发生了(如收到短信).类NotificationManager 用来处理Notification, NotificationManager可 ...