leetcode-374-Guess Number Higher or Lower(二分查找)

题目描述:

We are playing the Guess Game. The game is as follows:

I pick a number from 1 to n. You have to guess which number I picked.

Every time you guess wrong, I‘ll tell you whether the number is higher or lower.

You call a pre-defined API guess(int num) which returns 3 possible results (-11, or 0):

-1 : My number is lower
 1 : My number is higher
 0 : Congrats! You got it!

Example:

n = 10, I pick 6.

Return 6.

要完成的函数:

int guess(int num);//api函数

int guessNumber(int n)

说明:

1、给定一个整数 n,从1到n中挑一个数出来,让你猜是哪个数。每次你猜一个数,调用一次api函数,返回0就代表刚好是这个数,你猜对了!如果返回-1,就表示你猜的数太大了,实际的数比这个小。如果返回1,就表示你猜的数太小了,实际的数比这个大。要求最终返回对的那个数。

2、这道题很明显是一道二分查找的题目,手工写一个二分查找的代码。

代码如下:(附详解)

    int guess(int num);
    int guessNumber(int n)
    {
        int low=1,high=n,mid,t;
        while(low<=high)
        {
            mid=low+(high-low)/2;//写成 mid=(low+high)/2 可能会上溢
            t=guess(mid);
            if(t==0)
                return mid;
            else if(t==-1)//实际的数比mid小
                high=mid-1;
            else          //实际的数比mid大
                low=mid+1;
        }
    }

上述代码实测2ms,beats 100.00% of cpp submissions。

原文地址:https://www.cnblogs.com/king-3/p/9125248.html

时间: 2025-01-19 13:17:55

leetcode-374-Guess Number Higher or Lower(二分查找)的相关文章

Python [Leetcode 374]Guess Number Higher or Lower

题目描述: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number is higher or lower. You call a pre-defined API guess(int n

[leetcode] 374. Guess Number Higher or Lower

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number is higher or lower. You call a pre-defined API guess(int num) wh

374. Guess Number Higher or Lower

/* * 374. Guess Number Higher or Lower * 2016-7-17 by Mingyang */ public int guessNumber(int n) { if (guess(n) == 0) return n; int left = 1, right = n; while (left < right) { int mid = left + (right - left) / 2, t = guess(mid); if (t == 0) return mid

leetcode 375. Guess Number Higher or Lower II

传送门 375. Guess Number Higher or Lower II QuestionEditorial Solution My Submissions Total Accepted: 1546 Total Submissions: 5408 Difficulty: Medium We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess w

&lt;LeetCode OJ&gt; 374. Guess Number Higher or Lower

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number is higher or lower. You call a pre-defined API guess(int num) wh

【Leetcode】Guess Number Higher or Lower II

题目链接: 题目: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number I picked is higher or lower. However, when you guess a

374. Guess Number Higher or Lower java

We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the number is higher or lower. You call a pre-defined API guess(int num) wh

Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)

我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字.每次你猜错了,我会告诉你这个数字是大了还是小了.你调用一个预先定义好的接口 guess(int num),它会返回 3 个可能的结果(-1,1 或 0): -1 : 我的数字比较小 1 : 我的数字比较大 0 : 恭喜!你猜对了! 示例 : 输入: n = 10, pick = 6 输出: 6 相似题目:Leetcode之二分法专题-278. 第一个错误的版本(First Bad Version

LeetCode Guess Number Higher or Lower II

原题链接在这里:https://leetcode.com/problems/guess-number-higher-or-lower-ii/ 题目: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether

Guess Number Higher or Lower II--困惑

今天,试着做了一下LeetCode OJ上面的第375道题:Guess Number Higher or Lower II 原题链接:https://leetcode.com/problems/guess-number-higher-or-lower-ii/ 具体题目如下: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to guess which numb