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. TP无限回复

    引入文件和css样式 <script src="__PUBLIC__/bootstrap/js/jquery-1.11.2.min.js"></script> ...

  2. 在 python3.x中安装 Crypto 库

    1.安装:直接找过来 whl 安装:链接: https://pan.baidu.com/s/1zXjzchnqc1GgSWT9TjHDaA 提取码: dzbn 复制这段内容后打开百度网盘手机App,操 ...

  3. 字典排序 sorted

    a = {6:2,8:0,1:4,-5:6,99:11,4:22} print( sorted(a.items()) ) #默认安照key排序的print( sorted(a.items(),key= ...

  4. Python 语言来编码和解码 JSON 对象

    Json函数: json.dumps: Python标准库中的json模块,集成了将数据序列化处理的功能. 将 Python 对象编码成 JSON 字符串 语法: json.dumps(obj, sk ...

  5. Poj3624 Charm Bracelet (01背包)

    题目链接:http://poj.org/problem?id=3624 Description Bessie has gone to the mall's jewelry store and spie ...

  6. dotnet Core 异步任务

    使用线程池中线程的任务启动方式 线程池提供了一个后台线程的池,独自管理线程,按需增加或减少线程池中的线程数.线程池中的线程用于执行一些动作后仍然返回线程池中. using System; using ...

  7. MariaDB glare cluster简介

    MariaDB MariaDB 是由原来 MySQL 的作者Michael Widenius创办的公司所开发的免费开源的数据库服务器,MariaDB是同一MySQL版本的二进制替代品, 当前最新版本1 ...

  8. 也说性能测试,顺便说python的多进程+多线程、协程

    最近需要一个web系统进行接口性能测试,这里顺便说一下性能测试的步骤吧,大概如下 一.分析接口频率 根据系统的复杂程度,接口的数量有多有少,应该优先对那些频率高,数据库操作频繁的接口进行性能测试,所以 ...

  9. Shell脚本中的分号使用

    在Linux中,语句中的分号一般用作代码块标识 1.单行语句一般要用到分号来区分代码块,例如: if [ "$PS1" ]; then echo test is ok; fi te ...

  10. L2-003. 月饼

    L2-003. 月饼 月饼是中国人在中秋佳节时吃的一种传统食品,不同地区有许多不同风味的月饼.现给定所有种类月饼的库存量.总售价.以及市场的最大需求量,请你计算可以获得的最大收益是多少. 注意:销售时 ...