Leetcode 374. Guess Number Higher or Lower

文章作者:Tyan
博客:noahsnail.com  |  CSDN  |  简书

1. Description

Guess Number Higher or Lower

2. Solution

解析:二分查找问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution:
def guessNumber(self, n):
left = 1
right = n
while left <= right:
mid = (left + right) // 2
result = guess(mid)
if result > 0:
left = mid + 1
elif result < 0:
right = mid - 1
else:
return mid

Reference

  1. https://leetcode.com/problems/guess-number-higher-or-lower/
如果有收获,可以请我喝杯咖啡!