---
# System prepended metadata

title: 0277. Find the Celebrity
tags: [Leetcode, Medium, Microsoft]

---

# 0277. Find the Celebrity
###### tags: `Leetcode` `Microsoft` `Medium`
Link: https://leetcode.com/problems/find-the-celebrity/
## 思路
因为每次做一次check，就可以知道其中一个一定不是celebrity，那么只要做n-1次test，就会最终得到一个candidate，再去检测这个candidate是不是celebrity即可
## Code
```java=
public class Solution extends Relation {
    public int findCelebrity(int n) {
        int candidate = 0;
        for(int i = 0;i < n;i++){
            if(knows(candidate,i)){
                candidate = i;
            }
        }
        
        for(int i = 0;i < n;i++){
            if(candidate == i) continue;
            if(knows(candidate,i)||!knows(i,candidate)){
                return -1;
            }
        }
        return candidate;
    }
}
```