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 guess which number I picked.
我从1到n之间选择一个数字,由你来猜我选了哪个数字。
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
):
你可以用一个已经准备好的接口guess(int num),它会返回3个可能的结果(-1,1或0):
-1 : My number is lower //我的数字更小
1 : My number is higher //我的数字更大
0 : Congrats! You got it! //猜中了
刚开始第一次提交采用了二分查找,不过显示超时了。
超时case: 2126753390
1702766719
看讨论发现是溢出了,所以改了下程序。
// Forward declaration of guess API.
// @param num, your guess
// @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
int guess(int num); class Solution {
public:
int guessNumber(int n) {
int low=,high=n,g,r;
while(low<=high){
g=(low+high)/; //改成g=low+(high-low)/2;
r=guess(g);
if(r==-)high=g-;
else if(r==)low=g+;
else return g;
}
return ;
}
};
374. Guess Number Higher or Lower的更多相关文章
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
- 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 ...
- [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. 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. Guess Number Higher or Lower
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 We are ...
- 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 ...
- 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 ...
- 【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【leetcode❤python】 374. Guess Number Higher or Lower
#-*- coding: UTF-8 -*-# The guess API is already defined for you.# @param num, your guess# @return - ...
随机推荐
- linux集群时间同步
说明:由于hadoop集群对时间要求很高,所以集群内主机要经常同步.本文档适合ubuntu.redhat系列. 注:很多内容是在网上摘录,然后试验后总结,如有疑问可留言探讨. 1.设置主机时间准确(任 ...
- spring3.2.2+mybatis3.2.3+c3p0项目整合
国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...
- Spring Quartz结合Spring mail定期发送邮件
文件配置例如以下: <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="htt ...
- SAP增强总结-第二代增强(SMOD、CMOD)【转载】
第二代增强比第二代增强安全性提高了很多,第一代增强毕竟是在原标准程序中修改,大部分传递参数都可以直接使用,第二代增强做了一些封装,对用户可以修改的参数做了限制. 1.增强点查找方法 首先根据事物码找到 ...
- 网络防火墙实战-基于pfsense(2)
安装虚拟机 本博客所有内容是原创,如果转载请注明来源 http://blog.csdn.net/myhaspl/
- 并发与同步 (一) ThreadLocal与Synchronized 用哪一个好
ThreadLocal是什么? 历史 早在JDK 1.2的版本中就提供java.lang.ThreadLocal,ThreadLocal为解决多线程程序的并发问题提供了一种新的思路.使用这个工具类可以 ...
- Liunx下Tomcat+MYSQL+Nginx配置
环境:centos6.4 min #安装编译库及依赖模块 yum -y install gcc gcc-c++ autoconf automake make yum -y install zlib z ...
- Google maps API开发(一)(转)
一.加载Google maps API <script type="text/javascript" src="http://ditu.google.com/map ...
- 深入理解计算机系统第二版习题解答CSAPP 2.10
对于任一位向量a,有a ^ a = 0.考虑下面的程序: void inplace_swap(int *x, int *y) { *y = *x ^ *y; *x = *x ^ *y; *y = *x ...
- highcharts 24小时显示数据,显示00:00格式的数据
Showing 24 hours on xAxis WITHOUT a date on 00:00 格式参考PHP手册中的: strftime