众里寻他千百度:名流问题 :: labuladong的算法小抄 #1051
Replies: 11 comments 1 reply
-
第一次便利可以只调用一次 knows ,不需要调用两次: public int findCelebrity(int n) {
int curr = 0;
for(int i = 1; i<n; i++) if(knows(curr, i)) curr = i;
for(int i=0;i<n;i++)
if(i!=curr&&(knows(curr,i)||!knows(i,curr))) return -1;
return curr;
} |
Beta Was this translation helpful? Give feedback.
-
暴力求解法是不是有点问题,最后一个判断,我怎么跑的不对,感觉应当放到第二层循环里面 |
Beta Was this translation helpful? Give feedback.
-
暴力解法每次拿到know(candidate, other)却只用来判断candiate是否满足一定条件,以后还会在判断other的合法性时再次多触发一次,理论效率至少低一倍。 |
Beta Was this translation helpful? Give feedback.
-
关于文中笔误的问题
|
Beta Was this translation helpful? Give feedback.
-
@zhongweiLeex 感谢指出,已修复~ |
Beta Was this translation helpful? Give feedback.
-
文章中的题目要会员才能做,有个相同的题目不用会员也可以做,第997题【找到小镇的法官】 不过我感觉这类型的题目用出度和入度计算会比较简单一点,入度等于 |
Beta Was this translation helpful? Give feedback.
-
两人互不认识,都不是名人。只排除一个人而不是两个人,是为了让代码更具有统一性吧。 |
Beta Was this translation helpful? Give feedback.
-
check in |
Beta Was this translation helpful? Give feedback.
-
暴力解法中 for (int cand = 0; cand < n; cand++) { other 如何都会小于n 啊 怎么会满足判断条件 if (other == n) 的 ? |
Beta Was this translation helpful? Give feedback.
-
文章链接点这里:众里寻他千百度:名流问题
评论礼仪 见这里,违者直接拉黑。
Beta Was this translation helpful? Give feedback.
All reactions