当前位置:网站首页>leetcode:277. 搜寻名人
leetcode:277. 搜寻名人
2022-08-05 15:34:00 【OceanStar的学习笔记】
题目来源
题目描述
假设你是一个专业的狗仔,参加了一个 n 人派对,其中每个人被从 0 到 n - 1 标号。在这个派对人群当中可能存在一位 “名人”。所谓 “名人” 的定义是:其他所有 n - 1 个人都认识他/她,而他/她并不认识其他任何人。
现在你想要确认这个 “名人” 是谁,或者确定这里没有 “名人”。而你唯一能做的就是问诸如 “A 你好呀,请问你认不认识 B呀?” 的问题,以确定 A 是否认识 B。你需要在(渐近意义上)尽可能少的问题内来确定这位 “名人” 是谁(或者确定这里没有 “名人”)。
在本题中,你可以使用辅助函数 bool knows(a, b) 获取到 A 是否认识 B。请你来实现一个函数 int findCelebrity(n)。
派对最多只会有一个 “名人” 参加。若 “名人” 存在,请返回他/她的编号;若 “名人” 不存在,请返回 -1。
示例 1:
输入: graph = [
[1,1,0],
[0,1,0],
[1,1,1]
]
输出: 1
解析: 有编号分别为 0、1 和 2 的三个人。graph[i][j] = 1 代表编号为 i 的人认识编号为 j 的人,而 graph[i][j] = 0 则代表编号为 i 的人不认识编号为 j 的人。“名人” 是编号 1 的人,因为 0 和 2 均认识他/她,但 1 不认识任何人。
示例 2:
输入: graph = [
[1,0,1],
[1,1,0],
[0,1,1]
]
输出: -1
解析: 没有 “名人”
注意:
该有向图是以邻接矩阵的形式给出的,是一个 n × n 的矩阵, a[i][j] = 1 代表 i 与 j 认识,a[i][j] = 0 则代表 i 与 j 不认识。
请记住,您是无法直接访问邻接矩阵的。
题目解析
题目意思
- 什么是名人:其他人都认识祂,祂不认识任何人
- bool knows(a, b) :
- 如果返回true,表示a->b;那么a一定不是名人,因为名人不认识任何人
- 如果返回false,表示a不认识b:那么b一定不是名人,因为名人被所有人认识
思路
- 先设定候选人为res
- 然后遍历一遍,对于遍历到的人i,如果res->i,那么res不是名人,就将名人候选人设置为i…遍历完成之后,就找出了一个可能的候选人
- 然后我们再遍历一遍,来检测res是否真的是名人
class Solution {
public:
int findCelebrity(int n) {
// 找出一个可能的名人
int cand = 0;
for (int i = 1; i < n; ++i) {
if(knows(cand, i)){
cand = i;
}
}
// 验证cand是否是名人
// (1) cand不认识其他人(cand...右侧的都不认识,只需要验证...cand)
for (int i = 0; i < cand; ++i) {
if(knows(cand, i)){
return -1;
}
}
//(2) 其他人都认识cand
for (int i = 0; i < cand; ++i) {
if(!knows(i, cand)){
return -1;
}
}
return cand;
}
};
思路:
- 用一个一维数组来标记每一个的候选状态,一开始均初始化为true,表示每个人都是名人候选人
- 然后遍历数组
bool knows(int a, int b);
class Solution {
public:
int findCelebrity(int n) {
vector<bool> candidate(n, true);
for (int i = 0; i < n; ++i) {
//验证i是不是名人
for (int j = 0; j < n; ++j) {
if (candidate[i] && i != j) {
if (knows(i, j) || !knows(j, i)) {
candidate[i] = false;
break;
} else {
candidate[j] = false;
}
}
}
//
if (candidate[i]) return i;
}
return -1;
}
};
边栏推荐
- 学习笔记220—office2016无法登录账号,提示“很抱歉遇到一些临时服务器问题”?
- Further dissection and application of disappearing heritability
- 双因子与多因子身份验证有什么区别?
- If there are 10 words, and I want to take 3 words out of them, and record all possible stats for 3 out of 10, how do I do that?...
- 记一次对某站点详细的渗透测试
- 拉格朗日对偶问题
- Redis系列5:深入分析Cluster 集群模式
- 【构造方法概述 Objective-C中】
- 如何找回u盘里丢失的文件,u盘里的文件丢了怎么找回
- [Supplementary Question Diary] [2022 Hangzhou Electric Summer School Multi-School 3] K-Taxi
猜你喜欢
随机推荐
《安富莱嵌入式周报》第276期:2022.07.25--2022.07.3
EasyCVR calls the stop real-time recording interface, how to solve the problem that the recording address is not returned?
直播弹幕实现
Read it all!Adapter technology in NLP
JS--how to write event-driven
现在flink cdc有什么方式支持整库同步mysql吗
请指教同花顺究竟怎么开户?在线开户安全么?
对抗 | 利用de4dot解密被混淆的.NET代码
刷题《剑指Offer》day08
【Navicat】Navicat导出数据库表设计文档具体说明
1704. Determine if the two halves of a string are similar
高数_证明_极限存在的单调有界准则
Jmeter接口测试响应数据中文显示为Unicode码的解决方法
Is GF Futures Mobile Account Opening Safe?
1704. 判断字符串的两半是否相似
【构造方法概述 Objective-C中】
『攻防』记一次EDU攻防演练
Redis系列5:深入分析Cluster 集群模式
Locally boundedness of high number_proof_limit
[FlareOn5]Ultimate Minesweeper WP









