趣闻不设限专题5月一起学算法

一起学算法-374. 猜数字大小

2021-05-25  本文已影响0人  小杨同学97

一、题目

LeetCode-374. 猜数字大小
链接:https://leetcode-cn.com/problems/guess-number-higher-or-lower/

难度:简单
猜数字游戏的规则如下:

你可以通过调用一个预先定义好的接口 int guess(int num) 来获取猜测结果,返回值一共有 3 种可能的情况(-1,1 或 0):

示例 1:
输入:n = 10, pick = 6
输出:6

示例 2:
输入:n = 1, pick = 1
输出:1

示例 3:
输入:n = 2, pick = 1
输出:1

示例 4:
输入:n = 2, pick = 2
输出:2
 

提示:
1 <= n <= 2^31 - 1
1 <= pick <= n

二、解题思路

暴力枚举或者二分查找,把可能的值通过API去验证。
创建两个指针,left=1,right = n。
mid = left + (right - left)/2,通过api验证当前mid的值得到res。
如果res == 0,直接返回mid。
如果res == -1,说明我们猜的数大了,解在左半部分。
如果res == 1,说明我们猜的数小了,解在右半部分。

三、实现过程

c++

/** 
 * Forward declaration of guess API.
 * @param  num   your guess
 * @return       -1 if num is lower than the guess number
 *                1 if num is higher than the guess number
 *               otherwise return 0
 * int guess(int num);
 */

class Solution {
public:
    int guessNumber(int n) {
        int left = 1,right = n,mid,res;
        while(left <= right){
            mid = left + (right - left)/2;
            res = guess(mid);
            if(res == 0){
                break;
            }
            if(res < 0){
                right = mid - 1;
            }else{
                left = mid + 1;
            }
        }
        return mid;
    }
};

PHP

/** 
 * The API guess is defined in the parent class.
 * @param  num   your guess
 * @return       -1 if num is lower than the guess number
 *                1 if num is higher than the guess number
 *               otherwise return 0
 * public function guess($num){}
 */

class Solution extends GuessGame {
    /**
     * @param  Integer  $n
     * @return Integer
     */
    function guessNumber($n) {
        $left = 1;
        $right = $n;
    while($left <= $right){
        $mid = (int)($left + ($right - $left)/2);
        $res = $this->guess($mid);
        if($res == 0){
            return $mid;
        }
        if($res < 0){
            $right = $mid - 1;
        }else{
            $left = $mid + 1;
        }
    }
    }
}

JavaScript

/** 
 * Forward declaration of guess API.
 * @param {number} num   your guess
 * @return              -1 if num is lower than the guess number
 *                       1 if num is higher than the guess number
 *                       otherwise return 0
 * var guess = function(num) {}
 */

/**
 * @param {number} n
 * @return {number}
 */
var guessNumber = function(n) {
    var left = 1,right = n,mid,res;
    while(left <= right){
        mid = Math.floor(left + (right - left)/2);
        res = guess(mid);
        if(res == 0){
            return mid;
        }
        if(res < 0){
            right = mid - 1;
        }else{
            left = mid + 1;
        }
    }
};

四、小结

  1. 时间复杂度:O(logN),其中N是数字n的值。
  2. 空间复杂度:O(1)
上一篇 下一篇

猜你喜欢

热点阅读