这是悦乐书的第373次更新,第400篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第234题(顺位题号是997)。在一个城镇,有N个人从1到N标记。有传言说其中一个人是秘密的镇法官。

如果镇法官存在,那么:

  • 镇法官不信任任何人。

  • 每个人(镇法官除外)都信任镇法官。

  • 只有一个人满足前两条。

给定一个trust数组,一对trust[i] = [a,b]表示被标记为a的人信任标记为b的人。

如果镇法官存在并且可以识别,则返回镇法官的标签。否则,返回-1。

例如:

输入:N = 2,trust = [[1,2]]

输出:2

输入:N = 3,trust = [[1,3],[2,3]]

输出:3

输入:N = 3,trust = [[1,3],[2,3],[3,1]]

输出:-1

输入:N = 3,trust = [[1,2],[2,3]]

输出:-1

输入:N = 4,trust = [[1,3],[1,4],[2,3],[2,4],[4,3]]

输出:3

注意

  • 1 <= N <= 1000

  • trust.length <= 10000

  • trust[i]都是不同的。

  • trust[i][0] != trust[i][1]

  • 1 <= trust[i][0],trust[i][1] <= N

02 第一种解法

将题目翻译一下就是,法官的被信任次数等于N-1,并且法官不能信任其他人,即法官是trust数组中trust[i][1]出现次数等于N-1的人(被信任次数等于N-1),并且trust[i][0]不等于法官所在的标签(法官不能信任其他人)。

思路:利用HashMap记录被信任人出现的次数,找出其中被信任了N-1次的人,然后去trust数组中判断此人是否有信任过其他人。有种特殊情况N为1的时候,trust为空数组,即只有1个人,那么这个人就是法官。

public int findJudge(int N, int[][] trust) {
// 只有1个人的时候,法官就是他本人
if (N == 1) {
return N;
}
// key为被信任的人,value为其被信任的次数
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int[] arr : trust) {
map.put(arr[1], map.getOrDefault(arr[1], 0)+1);
}
// 找到被信任次数等于N-1的那个人
int count = -1;
for (Integer key : map.keySet()) {
if (map.get(key) == N-1) {
count = key;
}
}
// 被信任次数等于N-1的人,不能信任其他人
for (int[] arr : trust) {
if (arr[0] == count) {
return -1;
}
}
return count;
}

03 第二种解法

针对第一种解法中的HashMap,我们还可以用int数组进行替换,思路和上面第一种解法一致。

public int findJudge2(int N, int[][] trust) {
if (N == 1) {
return N;
}
int[] trusted = new int[N+1];
for (int i=0; i<trust.length; i++) {
trusted[trust[i][1]]++;
}
int count = -1;
for (int i=0; i<trusted.length; i++) {
if (trusted[i] == N-1) {
count = i;
}
}
for (int[] arr : trust) {
if (arr[0] == count) {
return -1;
}
}
return count;
}

04 第三种解法

针对第二种解法,我们还可以将其简化成2个for循环,将统计被信任次数和寻找被信任次数最多的人合在一起处理。

public int findJudge3(int N, int[][] trust) {
if (N == 1) {
return N;
}
int[] trusted = new int[N+1];
int count = -1, num = -1;
for (int i=0; i<trust.length; i++) {
trusted[trust[i][1]]++;
if (trusted[trust[i][1]] > count) {
count = trusted[trust[i][1]];
num = trust[i][1];
}
}
// 被信任次数要等于N-1
if (count != N-1) {
return -1;
}
for (int[] arr : trust) {
if (arr[0] == num) {
return -1;
}
}
return num;
}

05 第四种解法

我们还可以使用两个int数组来解,一个数组arr存信任的人,另一个数组arr2存被信任的人,找出被信任次数等于N-1arr2[i]=N-1)且没有信任过人(arr[i]=0)的人,他就是法官。

public int findJudge4(int N, int[][] trust) {
int[] arr = new int[N+1];
int[] arr2 = new int[N+1];
for (int i=0; i<trust.length; i++) {
arr[trust[i][0]]++;
arr2[trust[i][1]]++;
}
for (int j=1; j<arr.length; j++) {
if (arr[j] == 0 && arr2[j] == N-1) {
return j;
}
}
return -1;
}

06 小结

算法专题目前已连续日更超过七个月,算法题文章240+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode.997-找到镇法官(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 解题报告(C++)

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

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

  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_easy】997. Find the Town Judge

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

  6. 力扣(LeetCode) 997. 找到小镇的法官

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

  7. [LeetCode] 448. 找到所有数组中消失的数字(思维)

    题目 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现在数组中的数字. 您 ...

  8. Leetcode 658.找到K个最接近的元素

    找到k个最接近的元素 给定一个排序好的数组,两个整数 k 和 x,从数组中找到最靠近 x(两数之差最小)的 k 个数.返回的结果必须要是按升序排好的.如果有两个数与 x 的差值一样,优先选择数值较小的 ...

  9. Leetcode 448.找到所有数组中消失的数字

    找到所有数组中消失的数字 给定一个范围在  1 ≤ a[i] ≤ n ( n = 数组大小 ) 的 整型数组,数组中的元素一些出现了两次,另一些只出现一次. 找到所有在 [1, n] 范围之间没有出现 ...

随机推荐

  1. autofs 自动挂载.

    autofs 自动挂载. 操作环境:redhat 6 一.autofs 说明 自动挂载器是一个监视目录的守护进程,并在目标子目录被引用时,自动执行预定义的挂载 自动挂载器由autofs服务脚本管理 自 ...

  2. 【LuoguP5383】[模板]普通多项式转下降幂多项式

    传送门 Sol (怎么老是有人喜欢出新的多项式毒瘤板子,懒得整到一起了) 核心就是把 幂用下降幂来代替. 使用斯特林数展开幂为下降幂: \[x^n=\sum_{i=0}^n{x\choose i}i! ...

  3. 【leetcode】341. Flatten Nested List Iterator

    题目如下: Given a nested list of integers, implement an iterator to flatten it. Each element is either a ...

  4. 几种最常见的js array操作方法及示例

    1. 序言 操作array可谓前端最基础的工作,无论是从接口中取的数据,还是筛选数据,或者是添加按钮权限等等操作,array都是绕不开的东西.array的操作很多,初学者十分容易搞混,不是很熟练的情况 ...

  5. wx小程序知识点(四)

    四.页面间数据传递 和 参数传值 (1)页面间数据传递 ① 全局变量 ② 页面跳转或重定向时使用url携带参数(wx.navigateTo(urlStr)) ③ 使用组件模板 template < ...

  6. Beyong Compare 2.4.10刷新剩余日期bat批处理文件

    @echo off % start )(window.close)&&exit REG DELETE "HKCU\Software\Scooter Software\Beyo ...

  7. yiele函数

    1.概念: 当调用Thread.yield()函数时,会给线程调度器一个当前线程愿意让出CPU使用的暗示,但是线程调度器可能会忽略这个暗示. 2.实战. 看下面例子 public class yiel ...

  8. PHP 下载+安装

    1.官网下载 官网地址:http://PHP.net/ 地址:http://download.csdn.NET/detail/anndy_/9494632 官网手册:https://secure.ph ...

  9. axios多并发请求

    场景: 点击导出Excel按钮实现,姓名列表中前五个的所有的文章数据发送给后端,姓名列表中点击过的数据会被存放到localStorage中: 思路: 点击导出按钮,把前五个数据逐个和localStor ...

  10. 配置zookeeper开机自启动

    第一种.修改vim /etc/rc.local文件 vim /etc/rc.local 加入: export JAVA_HOME=/usr/local/JAVA/ --JDK安装路径/data/zoo ...