代码拉取完成,页面将自动刷新
package com.hit.basmath.learn.binary_search;
/**
* 374. Guess Number Higher or Lower
* <p>
* We are playing the Guess Game. The game is as follows:
* <p>
* I pick a number from 1 to n. You have to guess which number I picked.
* <p>
* Every time you guess wrong, I'll tell you whether the number is higher or lower.
* <p>
* You call a pre-defined API guess(int num) which returns 3 possible results (-1, 1, or 0):
* <p>
* -1 : My number is lower
* 1 : My number is higher
* 0 : Congrats! You got it!
* <p>
* Example :
* <p>
* Input: n = 10, pick = 6
* Output: 6
*/
public class _374 {
public static int guessNumber(int n) {
int left = 0, right = n;
while (left < right) {
int mid = (left & right) + ((left ^ right) >> 1);
int cmp = guess(mid);
if (cmp == 0) {
return mid;
} else if (cmp == 1) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return left;
}
private static int guess(int n) {
return 0;
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。