思路:
二分查找
# The guess API is already defined for you.
# @param num, your guess
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
# def guess(num):
class Solution(object):
def guessNumber(self, n):
"""
:type n: int
:rtype: int
"""
left=1
right=n
while left<=right:
mid=left+int(right-left)/2
if guess(mid)==0:
return mid
elif guess(mid)==-1:
right=mid-1
elif guess(mid)==1:
left=mid+1
# print(left,right)
写完老是通过不了,发现left==mid+1多写了个等号,细心啊