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):

Example

n = 10, I pick 4 (but you don't know)

Return 4. Correct !

注意计算中间节点的方法 (start+end)/2 和 start +(end-start)/2

前者对于Integer.MAX_VALUE溢出 后者ok

 /* 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 {
/**
* @param n an integer
* @return the number you guess
*/
public int guessNumber(int n) {
// Write your code here
int start = 1;
int end = n;
while(start<end){
int mid = start+(end-start)/2;
int res = guess(mid);
if(res==0) return mid;
else if(res==1) start = mid+1;
else end = mid-1;
}
return start;
}
}

Guess Number Game的更多相关文章

  1. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  2. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  3. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  4. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  5. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  6. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

  7. 有理数的稠密性(The rational points are dense on the number axis.)

    每一个实数都能用有理数去逼近到任意精确的程度,这就是有理数的稠密性.The rational points are dense on the number axis.

  8. [LeetCode] Minimum Number of Arrows to Burst Balloons 最少数量的箭引爆气球

    There are a number of spherical balloons spread in two-dimensional space. For each balloon, provided ...

  9. [LeetCode] Number of Boomerangs 回旋镖的数量

    Given n points in the plane that are all pairwise distinct, a "boomerang" is a tuple of po ...

  10. [LeetCode] Number of Segments in a String 字符串中的分段数量

    Count the number of segments in a string, where a segment is defined to be a contiguous sequence of ...

随机推荐

  1. docker构建本地仓库后,无法登陆解决办法(CentOS/Ubuntu)

    docker版本为:Server Version: 1.12.6 从dockerhub上下载最新的registry镜像. 首先.构建registry 1.下载registry镜像 docker pul ...

  2. [Android] websocket客户端开发

    为了能够在H5和APP都保持同一套长连接接口,因为采用websocket协议作为开发 使用的第三方库是:https://github.com/TakahikoKawasaki/nv-websocket ...

  3. Docker Kubernetes Service 网络服务代理模式详解

    Docker Kubernetes  Service 网络服务代理模式详解 Service service是实现kubernetes网络通信的一个服务 主要功能:负载均衡.网络规则分布到具体pod 注 ...

  4. mongodb主从(副本集附仲裁节点)部署带认证模式

    环境:OS:CentOS 7DB:3.0.15机器角色:192.168.1.134:10001 主192.168.1.135:10002 从192.168.1.135:10003 仲裁节点 1.下载相 ...

  5. Overture里如何添加震音记号

    五线谱里的震音记号是指当一个或数个音以相同的时值反复奏响,为了减少乐谱写作中的工作量,而使用的一种省略记号.震音记号用短斜线表示,斜线的数目与演奏时的符尾数目相同. 震音可以分为单震音和复震音两种.单 ...

  6. JDK命令行工具

    jinfo(Configuration Info for Java)的作用是实时地查看和调整虚拟机各项参数 jmap(Memory Map for Java)命令用于生成堆转储快照(一般称为heapd ...

  7. 牛客练习赛26—D xor序列 —线性基

    这是我第一次写关于线性基的题目.其实这题很好理解,先把给出的数能异或出的值给存在p数组里面,p[i]代表着该异或出的数的最高位为第i位且为1. 求出来后,再把x,y处理下,然后直接一位一位的判断是否为 ...

  8. FastJson中JSONObject用法及常用方法总结

    本文为博主原创,未经允许不得转载: 最近一直有用到解析各种数据,主要是用FastJson进行数据解析,其中一个重要的类为JSONObject,今天有时间,所以进行总结一下: JSONobject是Fa ...

  9. 【Python】【内置函数】

    [fromkeys()] -- coding: utf-8 -- python 27 xiaodeng python之函数用法fromkeys() fromkeys() 说明:用于创建一个新字典,以序 ...

  10. layui框架

    layui框架 Fly社区/分享一个layui风格的grid.greegrid