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. 简单的猜高猜低,运用二分法折半寻找。
在做题过程中,遇到了Time limit exceeded 百思不得其解
另外发现 自己对于一般的算法没有什么概念,每次做题都是从简单的数学原创方法开始,
找时间要去看一下算法的归纳,这样就可以在看见题目的同时知道对应的什么算法。
下面是程序:
// 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) {
if (guess(n) == 0) return n;
int left = 1, right = n;
while (left < right) {
int mid = left + (right - left) / 2, t = guess(mid);
if (t == 0) return mid;
else if (t == 1) left = mid;
else right = mid;
}
return left;
}
};
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 ...
- 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. Y ...
- 【一天一道LeetCode】#374. Guess Number Higher or Lower
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 We are ...
- 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 - ...
随机推荐
- C语言优先级
条件1 && 条件2,如果条件1为假,则条件2语句不执行,因为能判断该表达式为假 条件1 || 条件2,如果条件1为真,则条件2语句不执行,因为能判断该表达式为真 优先级 运算符 名称 ...
- samba基本应用24-4及示例
samba smb:service message block(137/udp, 138/udp, 139/tcp, 445/tcp) 协议是:CIFS:Common Internet File Sy ...
- python 操作浏览器打开指定网页
#! /usr/bin/env python # encoding=utf8 import webbrowser import time webbrowser.open("http://ww ...
- [ogre]ogre::Animation
参考:http://blog.csdn.net/leonwei/article/details/5819248 http://blog.csdn.net/debugconsole/article/de ...
- HDU 6114 Chess
Chess 思路:求C(n,m),除法取余用乘法逆元算. 代码: #include<bits/stdc++.h> using namespace std; #define ll long ...
- Java JDK5新特性-泛型
2017-10-30 22:47:11 Java 泛型(generics)是 JDK 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型. 泛型的本质 ...
- linux使用flock文件锁
使用linux flock 文件锁实现任务锁定,解决冲突 格式: flock [-sxun][-w #] fd# flock [-sxon][-w #] file [-c] command flock ...
- 1月24日 ruby基础3部分 Numeric, Array已学。
<div style="background:lightblue"> 第12章 数值类 12.1 数值的构成 Numeric-> Integer-> Fix ...
- Java 历史
James Gosling 最初开始 Java 语言项目是在 1991 年的 7 月.Java 被用在他的许多 set-top box 工程中.这个语言最开始的时候被称为 Oka,这个是因为 Jame ...
- python-day21--sys模块
sys模块是与python解释器交互的一个接口 1.sys.argv 命令行参数List,第一个元素是程序本身路径 # 传参 应用场景:权限控制 2.sys.path ...