LeetCode "Find the Celebrity"

Lesson learnt: Get rid of XX algorithm routines clogging your mind and focusenjoy the problem itself!

// Forward declaration of the knows API.
bool knows(int a, int b);

class Solution {
public:
    int findCelebrity(int n) 
    {
        //  Pass 1: pick out a potential candidate
        int cand = -1;
        for(int i = 0 ; i < n ; i ++)
        {
            if(cand == -1 || !knows(i, cand))
            {
                cand = i;
            }
        }
        
        //  Pass 2: db-check the cand
        for(int i = 0; i < n; i ++)
        {
            if(i == cand) continue;
            if(!knows(i, cand) || knows(cand, i))
                return -1;
        }
        
        return cand;
    }
};
原文地址:https://www.cnblogs.com/tonix/p/4799757.html