Skip to content

277. Find the Celebrity #58

Closed
Closed
@tech-cow

Description

@tech-cow

第一刷记录:

失败: 对input没搞清楚,题目的Example是graph,所以很顺其自然的想到创建Graph上面,我思路是创建一个graph,然后寻找没有neighbor的node,和下面的方法high level上没有太大的出路

class Solution(object):
    def findCelebrity(self, n):
        celeb = 0
        
        for i in range(1, n):
            if knows(celeb, i):
                celeb = i
            
        for i in range(n):
            if i == celeb: continue
            if knows(celeb, i): return -1
            if not knows(i, celeb): return -1
        
        return celeb

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions