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 ...
随机推荐
- informix(南大通用)sql语法的差异
1.create view 444(...) as select ...from... 2.insert into select.......union select 不支持 请分开写 ...
- thinkphp3.2.3 ueditor1.4.3 图片上传操作,在线删除上传图片功能。
最近弄一个图片 上传,可是用ueditor 自带的上传,如果不配置的话,上传的目录不在自己的项目中. 在网上找了好多,可是都是底版本的,新版本的还真是找到了一个,ueditor-thinkphp 这个 ...
- Day3监督学习——决策树原理
Day3 机器学习监督学习——决策树原理 一.决策树的原理 1.机器学习中分类和预测算法的评估: 准确率 速度 强壮型:有数据缺失或错误时算法的运行 可规模性:数量级规模比较大 可解释性 2.决策树( ...
- 监听outlook新邮件
using System; using System.Linq; using Microsoft.Office.Interop.Outlook; using System.Collections.Ge ...
- @Html.Partial,@Html.Action,@Html.RenderPartial,@Html.RenderAction [转]
@Html.Action:需要有对应的Action,并且Action方法有返回值.(注:处理完业务逻辑同时,也需要返回所需值) @{Html.RenderAction}:需要有对应的Action,Ac ...
- 浅谈windows.onload()与$(document).ready()
浏览器加载完DOM后,会通过javascript为DOM元素添加事件,在javascript中,通常使用window.onload()方法. 在jquery中,则使用$(document).ready ...
- Hibernate课程 初探一对多映射4-3 测试--信息查询
建立双向一对多关系,既可以由一方查询多方信息,同样可以由多方查询一方信息 demo: //查询学生所在班级 public static void showGidByStudent(){ Session ...
- vim基本操作思维导图
- oracle笔记1:-安装-体系结构-基本查询
一.Oracle数据库的安装(安装在虚拟机的xp系统里) 1.安装虚拟机 2.虚拟机内安装xp系统,以下步骤在虚拟机内进行 3.双击setup.exe,在出现的界面中设置口令, ...
- Android 5.0 以上监听网络变化
大家好,大概有一个多月没有更新博客了,我是干什么去了呢?很明显,程序员当然要加班……这一次跟大家分享一下新项目的一些心得. 监听网络变化在开发中是经常用到的,例如我们断网有一些友好的提示,或者根据不同 ...