Simple C++ code 179ms


  • 0
    S
    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;
        }
    };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.