```
class Solution{
public:
int findCelebrity(int n){
int i=0,j;
while(i<n){
for(j=i+1;j<n;j++)if(knows(i,j))break;
if(j==n)break;
i=j;
}
for(j=0;j<i;j++)if(knows(i,j))return -1;
for(j=0;j<n;j++)if(j!=i&&!knows(j,i))return -1;
return i;
}
};
```