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. 委托初级篇——lambda表达式的推导

    public delegate void ConsoleWriteStr(string name,DateTime now); public delegate int DelegateAdd(int ...

  2. WPF DesiredSize & RenderSize

    DesiredSize DesiredSize介绍 关于DesiredSize的介绍,可以查看最新微软文档对DesiredSize的介绍 DesiredSize,指的是元素在布局过程中计算所需要的大小 ...

  3. HTTP协议的六种请求方法

    抛砖引玉,聊下概念性的东西先: HTTP协议 (Hyper Text Transfer Protocol) HTTP是一个基于TCP/IP通信协议来传递数据,包括html文件.图像.结果等,即是一个客 ...

  4. JVM 调优参数解释

    典型配置: java -Xmx3800m -Xms3800m -Xmn2g -Xss128k -XX:+UseParallelGC -XX:ParallelGCThreads=20 -XX:+UseP ...

  5. es6的let,const

    1.es6 新增的let const 命令 let用来定义一个局部变量,故名思意就是只在当前代码块可用 1.1 let 声明的变量不存在变量提升(var 声明的变量存在变量提升)且代码块内 暂时性死区 ...

  6. 20190328-CSS样式一:字体样式font-、文本样式text-、背景图样式background-

    目录 CSS参考手册:http://css.doyoe.com/ 1.字体简写:font:font-style || font-variant || font-weight || font-size ...

  7. java-初识Properties

    1.通过代码了解一哈: package com.etc; import java.io.File; import java.io.FileInputStream; import java.io.Fil ...

  8. 51nod“省选”模测第二场 C 小朋友的笑话(线段树 set)

    题意 题目链接 Sol 直接拿set维护\(li\)连续段.因为set内的区间互不相交,而且每个线段会被至多加入删除一次,所以复杂度是对的. #include<bits/stdc++.h> ...

  9. Android ContenObserver 监听联系人数据变化

    一.知识介绍 1.ContentProvider是内容提供者 ContentResolver是内容解决者(对内容提供的数据进行操作) ContentObserver是内容观察者(观察内容提供者提供的数 ...

  10. PHP实现开心消消乐的算法示例

    本文主要介绍了关于PHP如何实现我们大家都知道的开心消消乐的算法,分享PHP教程出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧. 一.需求描述:      1.在一个8*8的矩阵方格中随机 ...