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.
思路就是二分查找法。
public int guessNumber(int n) {
int low = 1,

本文介绍了如何玩Guess Game,玩家需要从1到n中猜测我选择的数字。每次猜测错误,会得到是更高还是更低的提示。解决这个问题可以利用二分查找算法,通过guess(int num) API获取(-1, 1, 0)的返回值来逐步缩小范围。"
132687598,18614638,拔河比赛人选选择算法,"['Python', '排序算法', '数据结构', '华为机试题']
订阅专栏 解锁全文
1044

被折叠的 条评论
为什么被折叠?



