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 (-11, or 0):

-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!

Example:

n = 10, I pick 6.

Return 6.

解法一:  递归

public int guessNumber(int n) {
return guessNumber(1, n);
}
/*递归*/
private int guessNumber(int start,int end){
int mid=start+(end-start)/2;
if(guess(mid)==-1){
return guessNumber(start,mid-1);
}else
if(guess(mid)==1){
return guessNumber(mid+1,end);
}else{
return mid;
}
}
}

解法二:循环

public int guessNumber(int n) {

        int low = 1;
int high = n; while (low <= high) {
int mid = low+(high-low)/2;
int guessResult = guess(mid);
if (guessResult == 0)
return mid; if (guessResult == 1)
low = mid+1;
else if (guessResult == -1)
high = mid-1;
} return -1;
}

其他解法,参考:

374. Guess Number Higher or Lower

每天一道LeetCode--374. Guess Number Higher or Lower的更多相关文章

  1. leetcode 374. Guess Number Higher or Lower 、375. Guess Number Higher or Lower II

    374. Guess Number Higher or Lower 二分查找就好 // Forward declaration of guess API. // @param num, your gu ...

  2. [LeetCode] 374. Guess Number Higher or Lower 猜数字大小

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  3. LeetCode 374. Guess Number Higher or Lower

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  4. Python [Leetcode 374]Guess Number Higher or Lower

    题目描述: We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have t ...

  5. [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小 II

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  6. Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)

    Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...

  7. 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II

    好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...

  8. [LeetCode] 375. Guess Number Higher or Lower II 猜数字大小之二

    We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You have to gues ...

  9. 【一天一道LeetCode】#374. Guess Number Higher or Lower

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 We are ...

  10. 【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...

随机推荐

  1. 新唐M0 ISP下载要点

    http://blog.csdn.net/rejoice818/article/details/7736029 一.注意:官方光盘内“Software Utilities”目录下,可找到ICP或ISP ...

  2. uva539 The Settlers of Catan

    The Settlers of Catan Within Settlers of Catan, the 1995 German game of the year, players attempt to ...

  3. 【M31】让函数根据一个以上的对象类型来决定如何虚化

    1.考虑下面的问题,游戏软件中有角色A,B,角色又可以细化为A1,A2,A3:B1,B2,B3,两类角色之间相互攻击.即A1可以攻击B1,B2,B3,B1可以攻击A1,A2,A3.C++的多态,只根据 ...

  4. 从来没有天才 靠自己创造未来——Leo鉴书(29)

    之前在网上跟朋友们聊起天才这个话题,我认来从来没什么所谓天才,有朋友认为有的,只是我们定义不同,要不你看看苏轼? 持天才论者持两个观点:有些人天生擅长干某些事儿,也许是基因作怪:有些人的能力是上帝或者 ...

  5. Android定时器,推荐ScheduledThreadPoolExecutor

    Android定时器,推荐ScheduledThreadPoolExecutor 官方网址:http://developer.android.com/reference/java/util/Timer ...

  6. Android Activity界面切换添加动画特效(转)

    在Android 2.0之后有了overridePendingTransition() ,其中里面两个参数,一个是前一个activity的退出两一个activity的进入, @Override pub ...

  7. mongodb的分布式集群(4、分片和副本集的结合)

    概述 前面3篇博客讲了mongodb的分布式和集群,当中第一种的主从复制我们差点儿不用,没有什么意义,剩下的两种,我们不论单独的使用哪一个.都会出现对应的问题.比較好的一种解决方式就是.分片和副本集的 ...

  8. NBUT 1225 NEW RDSP MODE I

    找出循环周期即可了 #include<bits/stdc++.h> using namespace std; int N,M,X; int time(int x,int y,int z) ...

  9. [原创]-CMD命令设置IP地址

    问题描述 在实际工作中,尤其是像我们这种BI分析人员,在做项目的时候,时常都需要因客户的不同随时切换不同的网络环境,有时可能需要在公司和客户之间来回的穿梭.交替.问题也就随之而来:每次客户那里都需要设 ...

  10. [设计模式3]--工厂(Factory)模式

    原文出处:http://blog.csdn.net/lwbeyond/article/details/7528309 工厂模式属于创建型模式,大致可以分为三类,简单工厂模式.工厂方法模式.抽象工厂模式 ...