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. JavaScript 原型链学习(二)原型的动态性

    由于在原型中查找值的过程是一次搜索,因此我们对原型对象所做的任何修改都能够立即从实例上反映出来,即使是先创建了实例后修改原型也照样如此.如下示例: var friend = new Person(); ...

  2. Linux(5.5版为主)的基本操作命令

    mount  查看挂载目录 cat  ~       查看文件下的内容 touch ~       创建一个文件 一次性性创建几个文件:  touch  /tmp/{1,2,3,4}.txt     ...

  3. Maven Web Project设置Webcontent路径

    1,新建maven-archetype-webapp 2,右键项目-->Properties-->选中Project Facets中的Runtimes标签,然后Java版本改为1.8,Dy ...

  4. MVC设计思路

    MVC 学会重复.学会总结.学会预习和练习 前端页面  <---->   服务器(控制层.业务层.DAO层) <--->  DB 说明:无论是框架还是servletJSP,用的 ...

  5. Linux 简单文本处理

    1.创建文件加“.”带表隐藏文件 2.password文件内“user:x:501:501::/home/lishiming:/bin/bash”含义:   用户名:密码控位键:UID:GID:用户解 ...

  6. 2018-2019-1 20189206 《Linux内核原理与分析》第九周作业

    #linux内核分析学习笔记 --第八章 进程的切换和系统的一般执行过程 学习目标:重点关注进程切换的过程,进程调度的时机,操作系统的基本构成以及一般的执行过程. 进程调度的时机 因为进程的调度只发生 ...

  7. win7下Oracle库impdp导入dmp

    第一步:创建备份文件存储目录 create or replace directory back_file as 'D:\app\yangxf\back_or_memery_file'; create ...

  8. CSS布局学习(三) - position属性定义及解释(官网直译)

    static ①元素的位置是在文档正常布局流中的位置. ②设置top right bottom left与z-index无效. ③在未指定position时,static是默认值 以下例子进行说明: ...

  9. Nat Med:单独使用anti-CTLA4治疗前列腺癌效果差的原因

    肿瘤细胞能够分泌特定的细胞因子,结合T细胞表面的受体抑制其活性,从而来影响免疫细胞杀死肿瘤细胞的能力.这一类细胞因子被冠名为抗肿瘤免疫反应的“check point”.针对这类check point信 ...

  10. python+selenium,实现带有验证码的自动化登录功能

    python+selenium的环境准备,请自行安装完成,这里直接贴代码,方便做项目时直接使用. import time from selenium import webdriver from PIL ...