码迷,mamicode.com
首页 > 其他好文 > 详细

LeetCode "Find the Celebrity"

时间:2015-09-11 01:32:09      阅读:336      评论:0      收藏:0      [点我收藏+]

标签:

Lesson learnt: Get rid of XX algorithm routines clogging your mind and focus\enjoy 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;
    }
};

LeetCode "Find the Celebrity"

标签:

原文地址:http://www.cnblogs.com/tonix/p/4799757.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!