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 - ...
随机推荐
- VB操作Excel
在工程中添加引用:Microsoft Office 9.0 Object Library Microsoft Excel 9.0 Object Library ...
- 剑指OFFER之树的子结构(九度OJ1520)
题目描述: 输入两颗二叉树A,B,判断B是不是A的子结构. 输入: 输入可能包含多个测试样例,输入以EOF结束.对于每个测试案例,输入的第一行一个整数n,m(1<=n<=1000,1< ...
- Hibernate的BaseDao辅助类
1.BaseDao接口类,该类封装了一些hibernate操作数据库的一些常用的方法,包括分页查询,使用该类极大的简化了hibernate的开发 BaseDao.java package com.kj ...
- JSon实体类快速生成插件 GsonFormat 1.2.0
写在前头:本插件只适用 android studio和 Intellij IDEA 工具,eclipse 的少年无视我吧!!! 这是一个根据JSONObject格式的字符串,自动生成实体类参数. gi ...
- HBase 和 MongoDB在设计上的区别
转载:http://leongfans.iteye.com/blog/1019383 昨天搜一下mongodb的资料,介绍应用的比较多,原理介绍的不多. 粗略得看了一下,总体来说两者的设计思路差不多, ...
- int、bigint、smallint 和 tinyint
Transact-SQL 参考 int.bigint.smallint 和 tinyint 使用整数数据的精确数字数据类型. bigint 从 -2^63 (-9223372036854775808) ...
- android code bbs for developer
http://bbs.aiyingli.com/forum.php http://www.eoeandroid.com/ http://www.javaapk.com/demo http://www. ...
- SAP ABAP MARD和MARDH计算逻辑
mard里记载的是当前库存的数量,但是期间并不一定是当月.比如你物料4月一整月都没有库存数量变化(没收没发),那么5月初你看mard里的条目期间数还是4月而非5月. 当某个期间发生货物移动的时候,系统 ...
- PAT 1012
1012. The Best Rank (25) To evaluate the performance of our first year CS majored students, we consi ...
- vs2010问题 error MSB8008: 指定的平台工具集(v110)未安装或无效
vs2012上的项目导入到vs2010中,编译出现 >------ 已启动生成: 项目: HelloGame.win32, 配置: Debug Win32 ------ >生成启动时间为 ...