Find the Celebrity(双指针)
Example
2 // next n * (n - 1) lines
0 knows 1
1 does not know 0Notice
"""
The knows API is already defined for you.
@param a, person a
@param b, person b
@return a boolean, whether a knows b
you can call Celebrity.knows(a, b)
"""
class Solution:
# @param {int} n a party with n people
# @return {int} the celebrity's label or -1
def findCelebrity(self, n):
# Write your code here
s,e=0,n-1
while s<e:
if Celebrity.knows(s, e):
s+=1
else:
e-=1
for i in range(0,n):
if i == e or (Celebrity.knows(i, e) and not Celebrity.knows(e, i)):
continue
else:
return -1
return eLast updated