Leetcode 374. Guess Number Higher or Lower

2021-03-02 16:28:21 浏览数 (1)

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

1. Description

2. Solution

解析:二分查找问题。

代码语言:javascript复制
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/

0 人点赞