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 (-1, 1, 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.

大意:

我们玩一个猜数字游戏,方法如下:

我在1到n之间选一个数字,你来猜我选的是什么

每次你猜错了,我都会告诉你数字是大了还是小了

你可以调用预定义的 API guess(int num) ,它会返回三个可能的结果 (-1, 1, or 0):

-1 : 我的数字更小
1 : 我的数字更大
0 : 恭喜!猜中了!

例子:

n = 10, 我选 6.
返回6.

思路:

这道题题目主动提示了用二分法来做,所以只用把二分法的思想写出来,根据每次猜测得到的大了或者小了的结果来进行分别处理。猜小了就在大的那个区间去继续取中间数字猜,猜大了就在小的那个区间取中间数字猜,因为取中间数字猜整体来说是最快的,为了记录区间,还要保留上次猜的情况,来让区间越缩越小。

代码(Java):

/* The guess API is defined in the parent class GuessGame. @param num, your guess @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 int guess(int num); */

public class Solution extends GuessGame {
    public int guessNumber(int n) {
        return helper(1,n);
    }

    public int helper(int start, int end){
        if(start == end) return start;
        int mid = Math.toIntExact(((long)start+(long)end)/2);
        int r = 0;
        if(guess(mid) == 0) r = mid;
        else if(guess(mid) == 1) r = helper(mid+1, end);
        else if(guess(mid) == -1) r = helper(start, mid-1);
        return r;
    }
}

合集:https://github.com/Cloudox/LeetCode-Record
版权所有:http://blog.csdn.net/cloudox_

你可能感兴趣的:(LeetCode)