# LeetCode: 374. 猜数字大小¶

## 1、题目描述¶

-1 : 我的数字比较小
1 : 我的数字比较大
0 : 恭喜！你猜对了！


n = 10, 我选择 6.



## 2、解题思路¶

# The guess API is already defined for you.
# @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 = 0
right = n

while left < right - 1:
if guess(right - (right - left) // 2) == 1:
left = left + (right - left) // 2
elif guess(right - (right - left) // 2) == -1:
right = right - (right - left) // 2
elif guess(right - (right - left) // 2) == 0:
return right - (right - left) // 2

if guess(left) == 0:
return left
else:
return right

# The guess API is already defined for you.
# @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
mid = (left + right) // 2
while guess(mid) != 0:
if guess(mid) == 1:
left = mid + 1
else:
right = mid - 1
mid = (left + right) // 2

return mid