【leetcode73】经典算法-Guess Number Higher or Lower
题目描述:
从1~n中,随便的拿出一个数字,你来猜测。
提示
提供一个guess(int num)的api,针对猜测的数字,返回三个数值。0,-1,1
- 0;猜中返回num
- -1:比猜测的数值小
- 1:比猜测的数值大
例如:
n = 10, I pick 6.
Return 6.
原文描述:
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):
-1 : My number is lower
1 : My number is higher
0 : Congrats! You got it!
Example:
n = 10, I pick 6.
Return 6.
思路分析:
- 明显是二分法的应用,关于二分法,看我之前的一片博客,讲的比较详细,链接如下:
Java语言实现二分法
代码:
/* 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 {
public int guessNumber(int n) {
int L = 1,R = n;
while(L <= R){
int mid = L + ((R - L) >> 1);
int res = guess(mid);
if(res == 0) return mid;
else if(res == 1) L = mid + 1;
else R = mid - 1;
}
return L;
}
}
更多leetcode题目,请看我的leetcode专栏。链接如下:
我的微信二维码如下,欢迎交流讨论
欢迎关注《IT面试题汇总》微信订阅号。每天推送经典面试题和面试心得技巧,都是干货!
微信订阅号二维码如下:
【leetcode73】经典算法-Guess Number Higher or Lower的更多相关文章
- [LeetCode] 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 ...
- 374&375. Guess Number Higher or Lower 1&2
做leetcode的题 We are playing the Guess Game. The game is as follows: I pick a number from 1 to n. You ...
- [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 ...
- [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 ...
- [LeetCode] 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 ...
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- Guess Number Higher or Lower II--困惑
今天,试着做了一下LeetCode OJ上面的第375道题:Guess Number Higher or Lower II 原题链接:https://leetcode.com/problems/gue ...
- 每天一道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 ...
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
随机推荐
- jenkins部署.net平台自动化构建
在引入自动化部署工具的时候,我们对比了jenkins和gitlab CI,jenkins有非常丰富的插件,配置起来方便.gitlab CI更倾向于脚本配置,当然jenkins也可以使用pipeline ...
- Docker环境 ELK 快速部署
Docker环境 ELK快速部署 环境 Centos 7.4 , Docker version 17.12 Docker至少3GB内存: #内核配置 echo ' vm.max_map_count = ...
- 初识mybatis(二)
上篇博客我们介绍通过Java代码来创建mybatis的配置文件,港真,这种方式看起来有意思实际在开发中用的并不多,mybatis的配置还是以xml配置为主,本文我们就来看看如何通过xml文件来配置my ...
- 【mybatis深度历险系列】mybatis中的输入映射和输出映射
在前面的博文中,小编介绍了mybatis的框架原理以及入门程序,还有mybatis中开发到的两种方法,原始开发dao的方法和mapper代理方法,今天博文,我们来继续学习mybatis中的相关知识,随 ...
- iOS升级HTTPS通过ATS你所要知道的
由于苹果规定2017年1月1日以后,所有APP都要使用HTTPS进行网络请求,否则无法上架,因此研究了一下在iOS中使用HTTPS请求的实现.网上搜索了一些比较有用资料,大家可以参考下 苹果强制升级的 ...
- RAP在线接口管理统计部署
文档: https://github.com/thx/RAP/wiki/home_cn centos上部署 参考:https://github.com/thx/RAP/wiki/deploy_on_c ...
- Rxjava +Retrofit 你需要掌握的几个技巧,Retrofit缓存,RxJava封装,统一对有无网络处理,异常处理, 返回结果问题
本文出处 :Tamic 文/ http://blog.csdn.net/sk719887916/article/details/52132106 Rxjava +Rterofit 需要掌握的几个技巧 ...
- 在8X8的棋盘上分布着n个骑士,他们想约在某一个格中聚会。骑士每天可以像国际象棋中的马那样移动一次,可以从中间像8个方向移动(当然不能走出棋盘),请计算n个骑士的最早聚会地点和要走多少天。要求尽早聚会
在8X8的棋盘上分布着n个骑士,他们想约在某一个格中聚会.骑士每天可以像国际象棋中的马那样移动一次,可以从中间像8个方向移动(当然不能走出棋盘),请计算n个骑士的最早聚会地点和要走多少天.要求尽早聚会 ...
- Redis集群功能预览
目前Redis Cluster仍处于Beta版本,Redis 3.0将会加入,在此可以先对其主要功能和原理进行一个预览.参考<Redis Cluster - a pragmatic approa ...
- [django]用fastcgi部署
django官方已经开始弃用fastcgi来部署django应用了,作为以前使用过的用户,还是贴一个配置,用来做纪念吧.. 项目下 #! /bin/sh case "$@" in ...