题目

在一个小镇里,按从 1 到 N 标记了 N 个人。传言称,这些人中有一个是小镇上的秘密法官。

如果小镇的法官真的存在,那么:

  1. 小镇的法官不相信任何人。
  2. 每个人(除了小镇法官外)都信任小镇的法官。
  3. 只有一个人同时满足属性 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. 1 <= N <= 1000
  2. trust.length <= 10000
  3. trust[i] 是完全不同的
  4. trust[i][0] != trust[i][1]
  5. 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的更多相关文章

  1. [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 ...

  2. #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  ...

  3. 【Leetcode_easy】997. Find the Town Judge

    problem 997. Find the Town Judge solution: class Solution { public: int findJudge(int N, vector<v ...

  4. 【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 ...

  5. 【LeetCode】997. Find the Town Judge 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 度 日期 题目地址:https://leetcode ...

  6. LeetCode题解之 Find the Town Judge

    1.题目描述 2.问题分析 使用map set数据结构. 3.代码 int findJudge(int N, vector<vector<int>>& trust) { ...

  7. LeetCode.997-找到镇法官(Find the Town Judge)

    这是悦乐书的第373次更新,第400篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第234题(顺位题号是997).在一个城镇,有N个人从1到N标记.有传言说其中一个人是秘 ...

  8. [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 ...

  9. LeetCode Find the Celebrity

    原题链接在这里:https://leetcode.com/problems/find-the-celebrity/ 题目: Suppose you are at a party with n peop ...

随机推荐

  1. python3+Appium自动化13-H5元素定位实践案例

    测试场景 启动钉钉app进入工作H5页面,点击考勤签到 查看webview上元素 1.电脑上打开chrome浏览器输入:chrome://inspect/#devices 2.Discover USB ...

  2. 性能测试工具LoadRunner04-LR之浏览器打不开

    环境:win7+lr11 IE浏览器要在9以下,9以上lr11是调不起来的 火狐浏览器,我用的是28,最新版本的也调不起来 可以调起浏览器但没有事件? 1.把INTERNET高级设置中的“启用第三方浏 ...

  3. 使用mongoosejs链接Mongodb

    以前只是了解Nodejs 这回打算好好学学.学到熟练使用的程度 var options={ user:'test_user', pass:'123456' }; mongoose.connect('m ...

  4. NASM在Ubuntu上的安装与简单使用

    一 .安装NASM 1. 下载安装文件 地址是:http://www.nasm.us/pub/nasm/releasebuilds/2.11.08/ 2.解压(具体命令要根据压缩包的类型来选用) 3. ...

  5. MVC execl 导入导出

    MVC execl 导入导出 <input id="xls" name="xls" type="file" style="w ...

  6. ORACLE 查看表空间

    select tablespace_name, file_id, file_name,round(bytes/(1024*1024),0) total_spacefrom dba_data_files ...

  7. 斗鱼连接弹幕Demo_pythonC#

    简明扼要的说下, 就两个线程,一个 负责收数据,一个负责发心跳包. 步骤如下, 进程1,调用 发包函数,发送连接请求,然后再发送 获取弹幕类型请求,就一直循环接收数据. 进程2,循环函数,每隔45秒向 ...

  8. javaSE练习3——数组

    一.编写一个简单程序,要求数组长度为5,分别赋值10,20,30,40,50,在控制台输出该数组的值. package com.test; public class t01 { public stat ...

  9. <Android 基础(八)> Palette

    介绍 Palette, 英文翻译,调色板,意思比较接近,Google给它的定位应该是颜色萃取器. 看下Source Code Palette , A helper class to extract p ...

  10. LeeCode 第1题

    要求: 给定一个整数(int)数组(Array)和一个目标数值(Target),找出数组中两数之和等于目标值(target)的两个元素的下标位置, 假设:结果唯一,数组中元素不会重复. 本人思路:分别 ...