题目

在一个小镇里,按从 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. vue脚手架 && 实例

    什么是vue脚手架? 即可以快速构建vue项目的工具,通过它,我们可以将vue项目所需要的文件等安装完成. 为什么要是用vue脚手架,优点何在? 因为创建一个vue项目需要很多的依赖,文件的设置等,而 ...

  2. 【转】Android实现伸缩弹力分布菜单效果

    本文介绍下在Android中实现伸缩弹力分布菜单效果.关于这种菜单效果在IPhone中比较常见,效果比较酷.那么在Android中实现只是一种简单的模仿. 这两天无意间看到一园友的博文实现Path2. ...

  3. 延迟查询--LINQ

    1.LINQ查询使用的是延迟查询的方法,以便提高效率 public static IEnumerable<TSource> Where<TSource>(this IEnume ...

  4. Oracle入门基础(1)

    1.数据库系统和数据管理系统的区别? 数据库系统=数据库的管理系统+oper操作员+硬件 2.Oracle的版本   8i /9i   10g/11g   12c(cloud) 3.Oracle主要组 ...

  5. 如何修改eclipse的web项目默认浏览器

  6. Fibonacci(斐波那契数列)的第N位数

    无穷数列1,1,2,3,5,8,13,21,34,55...称为Fibonacci数列,它可以递归地定义为F(n)=1 ...........(n=1或n=2)F(n)=F(n-1)+F(n-2).. ...

  7. url长度

    今天在测试Email Ticket的时候发现在进行Mark as Read/Unread操作时,请求是通过GET方式进行的.URL中列出了所有参与该操作的Ticket Id.于是,我想起GET请求是有 ...

  8. IO文件操作

    × 目录 [1]IO文件的操作 [2]Directory类 [3]File类 [4]FileStream类 [5]文本文件的操作 一.IO文件的操作:   .net中对文件操作,经常会用到这样几个类: ...

  9. oracle中文乱码问题

    1.查看服务器端编码select userenv('language') from dual;我实际查到的结果为:AMERICAN_AMERICA.ZHS16GBK 2.执行语句 select * f ...

  10. struts2.3.4.1转换成eclipse项目的过程

    1.在本地安装配置maven. 1.1.从maven官网下载,官网地址:http://maven.apache.org/download.cgi 1.2.配置maven环境变量       例如,我的 ...