leetcode 374
这个题目很简单,但是要注意细节和对题目的理解,一开始我把guess函数的作用理解错了,第一版代码长这样:
int guessNumber(int n)
{
int low = ;
int high = n; while(low < high)
{
int mid = (high + low) / ;
int g = guess(mid);
if(g == )
return mid;
else if(g > )
low = mid + ;
else
high = mid - ;
} return low; }
在重新读了几次题目之后,第二版代码长这样:
int guessNumber(int n)
{
int low = ;
int high = n; while(low < high)
{
int mid = (high + low) / ;
int g = guess(mid);
if(g == )
return mid;
else if(g < )
low = mid + ;
else
high = mid - ;
} return low; }
但是还是不能正确通过,这很尴尬:查了半天发现是上面标注的那段代码有问题,我一直以为把代码写成最简单的数学表达式是一个好选择,但是这里显然不是这样:因为这样当high和low都比较大时,这是会导致溢出!所以有了第三个版本的代码:
int guessNumber(int n)
{
int low = ;
int high = n; while(low < high)
{
int mid = low + (high - low) / ;
int g = guess(mid);
if(g == )
return mid;
else if(g < )
low = mid + ;
else
high = mid - ;
}
return low;
}
leetcode 374的更多相关文章
- [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 、375. Guess Number Higher or Lower II
374. Guess Number Higher or Lower 二分查找就好 // Forward declaration of guess API. // @param num, your gu ...
- Java实现 LeetCode 374 猜数字大小
374. 猜数字大小 我们正在玩一个猜数字游戏. 游戏规则如下: 我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字. 每次你猜错了,我会告诉你这个数字是大了还是小了. 你调用一个预先定义好的 ...
- 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_Easy tag: Binary Search
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. 猜数字大小(python)
我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你需要猜我选择了哪个数字.每次你猜错了,我会告诉你这个数字是大了还是小了.你调用一个预先定义好的接口 guess(int n ...
- leetcode 374猜数字大小
// Forward declaration of guess API. // @param num, your guess // @return -1 if my number is lower, ...
- Java实现 LeetCode 374 猜数字大小 II
375. 猜数字大小 II 我们正在玩一个猜数游戏,游戏规则如下: 我从 1 到 n 之间选择一个数字,你来猜我选了哪个数字. 每次你猜错了,我都会告诉你,我选的数字比你的大了或者小了. 然而,当你猜 ...
随机推荐
- Codeforces Beta Round #8
A题,小小的模拟题,没看懂题意啊. #include <iostream> #include <cstdio> #include <cmath> #include ...
- 【CodeVS】p1174 靶形数独
题目描述 Description 小城和小华都是热爱数学的好学生,最近,他们不约而同地迷上了数独游戏,好胜的他们想用数独来一比高低.但普通的数独对他们来说都过于简单了,于是他们向Z 博士请教,Z 博士 ...
- Pop3_解决PKIX:unable to find valid certification path to requested target 的问题
最近有公司pop3协议接收pp邮箱出现异常,连不上服务器,错误内容: e: sun.security.validator.ValidatorException: PKIX path building ...
- PHP 解决nginx 用file_get_content 问题
$my_curl = curl_init(); //初始化一个curl对象 curl_setopt($my_curl, CURLOPT_URL, "http://www.webjoy.net ...
- JSON value
JSON values can be: A number (integer or floating point) A string (in double quotes) A Boolean (true ...
- 浅析-博客Ping服务
简介:PING服务是博客站点向博客目标网站.搜索引擎等发出的博客内容更新通知服务,然后博客目标网站.搜索引擎就会及时的索引.收录以及传播您的博客内容. PING原理 PING 服务是博客站点向博客目标 ...
- 【android应用市场】android应用市场集合
一.举例 1.酷市场 2.apkpure 国外的应用市场,可不用FQ,没有google play的一些限制 相当于google play的镜像,可以下载google play的应用 3.360手机助手 ...
- Odoo 9 Odoo $ JQuery undifned
浏览器处于假死状态,查看console发现 odoo,jquery,$ 未定义三处错误,后台显示IOError: IOError: [Errno 2] No such file or director ...
- JAVA正则表达式介绍和使用
本文引用自 http://www.cnblogs.com/android-html5/archive/2012/06/02/2533924.html 技术博客 1.Java中在某个字符串中查询某个字符 ...
- CSS3初学篇章_4(边框样式/段落样式)
边框样式 1.边框线语法:border-style : none | hidden | dotted | dashed | solid | double | groove | ridge | inse ...