LeetCode知识点总结 - 997
LeetCode 997. Find the Town Judge
考点 | 难度 |
---|---|
Hash Table | Easy |
题目
In a town, there are n
people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.
If the town judge exists, then:
The town judge trusts nobody.
Everybody (except for the town judge) trusts the town judge.
There is exactly one person that satisfies properties 1 and 2.
You are given an array trust where trust[i] = [ai, bi]
representing that the person labeled ai trusts the person labeled bi.
Return the label of the town judge if the town judge exists and can be identified, or return -1
otherwise.
思路
用一个array储存所有人被trust - trust的结果(trust别人-1,被trust+1),最后遍历array找到值为n-1
的位置。
答案
class Solution {public int findJudge(int n, int[][] trust) {if(trust.length == 0){return n == 1 ? 1 : -1;}int[] trustCount = new int[n+1];for(int[] t : trust){trustCount[t[1]]++;trustCount[t[0]]--;}for(int i = 1; i < trustCount.length;i++){if(trustCount[i] == n-1)return i;}return -1;}
}
LeetCode知识点总结 - 997相关推荐
- LeetCode知识点总结 - 884
LeetCode 884. Uncommon Words from Two Sentences 考点 难度 Hash Table Easy 题目 A sentence is a string of s ...
- LeetCode知识点总结 - 868
LeetCode 868. Binary Gap 考点 难度 Math Easy 题目 Given a positive integer n, find and return the longest ...
- LeetCode知识点总结 - 1413
LeetCode 1413. Minimum Value to Get Positive Step by Step Sum 考点 难度 Prefix Sum Easy 题目 Given an arra ...
- LeetCode知识点总结 - 2073
LeetCode 2073. Time Needed to Buy Tickets 考点 难度 Simulation Easy 题目 There are n people in a line queu ...
- LeetCode知识点总结 - 347
LeetCode 347. Top K Frequent Elements 考点 难度 Array Medium 题目 Given an integer array nums and an integ ...
- LeetCode知识点总结 - 606
LeetCode 606. Construct String from Binary Tree 考点 难度 String Easy 题目 Given the root of a binary tree ...
- LeetCode知识点总结 - 844
LeetCode 844. Backspace String Compare 考点 难度 Simulation Easy 题目 Given two strings s and t, return tr ...
- LeetCode知识点总结 - 977
LeetCode 977. Squares of a Sorted Array 考点 难度 Sorting Easy 题目 Given an integer array nums sorted in ...
- LeetCode知识点总结 - 377
LeetCode 377. Combination Sum IV 考点 难度 DP Medium 题目 Given an array of distinct integers nums and a t ...
最新文章
- Redis知识点汇总
- 设置 页面 内容并排平均显示
- python的可变对象和不可变对象
- javascript --- vue2.x中原型的使用(拦截数组方法) 响应式原理(部分)
- 我改了500个Bug,但是!!
- 前端学习(2022)vue之电商管理系统电商系统之创建order分支
- jQuery基本使用
- 选择日期保存报错Invalid JSON input: Cannot deserialize value of type `java.util.Date` from String......
- rxjs为什么用的人少_工伤为什么公司不怕打官司
- 小谈EasyUI中的tree用法
- 用例子来区分4种文法
- oreo另一个意思_other和another区别:两种不同的“另一个”
- 几何分布的期望和方差公式推导_数学期望、方差、协方差
- Oracle 常用系统数据查询SQL(转)
- 如何让应用出现在LINUX的右键/打开方式/更多应用
- 结巴分词有前空格_结巴分词详细讲解
- UEFI shell - 标准应用程序的编译和加载过程
- python计算程序运行时间毫秒_Python获取秒级时间戳与毫秒级时间戳方法
- 2014全国计算机等级考试四级数据库工程师考试大纲,全国计算机等级考试四级数据库工程师...
- java calculator类_java里使用面向对象的思想编写一个计算器类(Calculator),可以实现两个数的加、减、乘、除运算...
热门文章
- 加速电脑启动,给电脑瘦身
- 打算的亲爱额请问请问额
- 网络推广是竞价好,还是优化好
- thinkpad 重装--AHCI 导致系统蓝屏---迅盘
- java.sql.SQLSyntaxErrorException: Table ‘XXX‘ doesn‘t exist
- 如何在线进行PDF文档解密操作
- 计算机网络—IP头部结构,TCP头部结构,UDP头部结构
- 中国的手机支付在全球遥遥领先
- oracle both from,Oracle trim函数的使用
- Qt Excel操作