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 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.
一个二分查找
// 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) {
long long start=,end=n;
long long mid=(start+end+)/;
if(==guess(start))return start;
while(guess(mid))
{
if(-==guess(mid))
{
end=mid-;
mid=(start+end+)/;
}
if(==guess(mid))
{
start=mid+;
if(==guess(start))return start;
mid=(start+end+)/;
}
}
return mid;
}
};
LeetCode 374. Guess Number Higher or Lower的更多相关文章
- 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 ...
- 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] 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之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...
- 不一样的猜数字游戏 — leetcode 375. Guess Number Higher or Lower II
好久没切 leetcode 的题了,静下心来切了道,这道题比较有意思,和大家分享下. 我把它叫做 "不一样的猜数字游戏",我们先来看看传统的猜数字游戏,Guess Number H ...
- [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】#374. Guess Number Higher or Lower
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 We are ...
- 【LeetCode】374. Guess Number Higher or Lower 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
随机推荐
- 系统弹性概念[TODO]
系统弹性 Shopify构建分布式可扩展应用的最佳实践 [编者的话]在构建大型分布式系统应用时,如何降低不同部分之间的依赖,增强系统的弹性,电商解决方案提供商 Shopify 给出了解决方法. 弹性矩 ...
- FTP应答码&响应码
2016-06-16 00:57:25 110: 重新启动标记应答. 120: 在n分钟内准备好 125: 连接打开准备传送 150: 打开数据连接200: 命令成功202: 命令失败211: 系统状 ...
- MySQL数据库之------DOS命令行的基本操作
1. 进入D盘的如下路径: 按住 ctrl+shift ,右键,选择在此处打开命令行窗口.出现图2. 图 1 图 2 2. . 3. 图 3 4. 图 4 ...
- Linux下VI命令详细介绍
vi 是"Visual Interface" 的简称,它在Linux 上的地位就仿佛Edit 程序在DOS上一样.它可以执行输出.删除.查找.替换.块操作等众多文本操作,而且 ...
- PHP MVC
学习一个框架之前,基本上我们都需要知道什么是mvc,即model-view-control,说白了就是数据控制以及页面的分离实现,mvc就 是这样应运而生的,mvc分为了三个层次,而且三个层次各司其职 ...
- Advanced Collection Views and Building Custom Layouts
Advanced Collection Views and Building Custom Layouts UICollectionView的结构回顾 首先回顾一下Collection View的构成 ...
- DOm4解析xml
1.创建XML文档对象的的方式有两种 1)Document document=DocumentHelper.createDocument(); 2)DocumentFactory documentFa ...
- 备忘DES简单加密与解密
package com.ego.util; import java.io.IOException; import java.security.SecureRandom; import javax.cr ...
- $.post() 传递多个参数.
$("#button").click(function() { /获取表单中id为idname和count的文本值付给property的两个属性 var property={&qu ...
- assign、copy 、retain等关键字的含义
assign: 简单赋值,不更改索引计数copy: 建立一个索引计数为1的对象,然后释放旧对象retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数为1Copy其实是建立了一个 ...