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, or0):
1234 >-1 : My number is lower> 1 : My number is higher> 0 : Congrats! You got it!>
>
Example:
1234 >n = 10, I pick 6.>>Return 6.>
解释
本题的情景是猜数字,给定1~n的数组,在猜的过程中,题目自带的API会提示数字是大了,还是小了,还是刚好猜中。
理解
本题有歧义:我对My number is lower 的理解是:我猜的数字小了,可是实际上题目对这句话的解读是:题目的数字小于我猜测的数字……
所以,刚开始完成二分查找算法,检查了很多遍算法本身并没有什么问题,可是结果总是不对,原来是这个原因,晕……
我的解法
|
|
大神解法
都是二分查找算法。