You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/** * Forward declaration of guess API. * @param {number} num your guess * @return -1 if num is lower than the guess number * 1 if num is higher than the guess number * otherwise return 0 * var guess = function(num) {} *//** * @param {number} n * @return {number} */varguessNumber=function(n){letleft=1;letright=n;while(left<=right){constmid=Math.floor((left+right)/2);constmidVal=guess(mid);if(midVal===0)returnmid;if(midVal===-1){right=mid-1;}elseif(midVal===1){left=mid+1;}}};
374. 猜数字大小
猜数字游戏的规则如下:
每轮游戏,系统都会从 1 到 n 随机选择一个数字。 请你猜选出的是哪个数字。
如果你猜错了,系统会告诉你这个数字比系统选出的数字是大了还是小了。
你可以通过调用一个预先定义好的接口 guess(int num) 来获取猜测结果,返回值一共有 3 种可能的情况(-1,1 或 0):
示例:
The text was updated successfully, but these errors were encountered: