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:

  1. The town judge trusts nobody.
  2. Everybody (except for the town judge) trusts the town judge.
  3. 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. 1 <= N <= 1000
  2. trust.length <= 10000
  3. trust[i] are all different
  4. trust[i][0] != trust[i][1]
  5. 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的更多相关文章

  1. 【Leetcode_easy】997. Find the Town Judge

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

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

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

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

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

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

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

  6. LeetCode题解之 Find the Town Judge

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

  7. LeetCode997. Find the Town Judge

    题目 在一个小镇里,按从 1 到 N 标记了 N 个人.传言称,这些人中有一个是小镇上的秘密法官. 如果小镇的法官真的存在,那么: 小镇的法官不相信任何人. 每个人(除了小镇法官外)都信任小镇的法官. ...

  8. 算法与数据结构基础 - 图(Graph)

    图基础 图(Graph)应用广泛,程序中可用邻接表和邻接矩阵表示图.依据不同维度,图可以分为有向图/无向图.有权图/无权图.连通图/非连通图.循环图/非循环图,有向图中的顶点具有入度/出度的概念. 面 ...

  9. Swift LeetCode 目录 | Catalog

    请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift    说明:题目中含有$符号则为付费题目. 如 ...

随机推荐

  1. 服务器控件的几个属性 SelectedIndex、SelectedItem、SelectedValue、SelectedItem.Text、selectedItem.value

    转自http://blog.csdn.net/iqv520/article/details/4419186 1. SelectedIndex ——选项的索引,为int,从0开始,可读可写 2. Sel ...

  2. Linux 安装composer

    wget https://getcomposer.org/installer //下载一个脚本文件 php installer //php执行下这个php脚本(虚拟机我没装环境.以下截图有操作流程) ...

  3. python爬虫项目-爬取雪球网金融数据(关注、持续更新)

    (一)python金融数据爬虫项目 爬取目标:雪球网(起始url:https://xueqiu.com/hq#exchange=CN&firstName=1&secondName=1_ ...

  4. HTML代码片段

    按钮类 后退.前进按钮 返回按钮 几种刷新按钮 警告框显示源代码 链接按钮 打开新窗口 打印 新窗口延迟打开 背景色变换 表单类 点击清空文字 关闭输入法 链接 双击打开链接 超链接鼠标形状 页面 不 ...

  5. 面试题之(vue生命周期)

    在面试的时候,vue生命周期被考察的很频繁. 什么是vue生命周期呢? Vue实例有一个完整的生命周期,也就是从开始创建.初始化数据.编译模板.挂载Dom.渲染→更新→渲染.卸载等一系列过程,我们称这 ...

  6. 在OQL上使用UPDLOCK锁定查询结果,安全的更新实体数据

    SqlServer查询记录的时候提供多种锁定方式,其中UPDLOCK 的优点是允许您读取数据(不阻塞其它事务)并在以后更新数据,同时确保自从上次读取数据后数据没有被更改.当我们用UPDLOCK来读取记 ...

  7. 轨迹系列5——验证轨迹GPS坐标转换为本地坐标的四/七参数是否准确的一种方案

    文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/ 1. 背景 目前对多个项目轨迹不准确的情况做了排查,发现导致轨迹偏移百分 ...

  8. Android 为TV端助力

    记录两次事情: 第一个给view添加动画效果,需要保证view是可以获取焦点的 第二个给listview,GridView设置选择器 listselector时,要保证他的子item无背景,否则选择器 ...

  9. 支持JSP和Servlet的Web服务器

    支持JSP和Servlet的Web服务器 1.Tomcat 服务器 目前非常流行的Tomcat服务器是Apache-Jarkarta开源项目中的一个子项目,是一个小型.轻量级的支持JSP和Servle ...

  10. 电脑一键U盘启动快捷键

    下面是我特意列出的品牌电脑.笔记本电脑.组装电脑一键U盘启动快捷键对应列表,仅供大家查阅参考! [品牌-笔记本电脑] 笔记本品牌  启动按键 联想笔记本  F12 宏基笔记本  F12 华硕笔记本   ...