[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 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. 思路也是典型的Binary Search, 只是需要注意的是guess(num) 每次对应的是猜的数高了, 还是答案高了即可. Code
# The guess API is already defined for you.
# @param num, your guess
# @return -1 if my number is lower, 1 if my number is higher, otherwise return 0
# def guess(num): class Solution(object):
def guessNumber(self, n):
l, r = 1, n
while l + 1 < r:
mid = l + (r-l)//2
num = guess(mid)
if num == 1:
l = mid
elif num == -1:
r = mid
else:
return mid
return l if guess(l) == 0 else r
[LeetCode] 374. Guess Number Higher or Lower_Easy tag: Binary Search的更多相关文章
- 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 ...
- 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] 278. First Bad Version_Easy tag: Binary Search
You are a product manager and currently leading a team to develop a new product. Unfortunately, the ...
- [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] 33. Search in Rotated Sorted Array_Medium tag: Binary Search
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
随机推荐
- ajax 获取服务器返回的XML字符串
前台 解析失败不会抛出任何异常, 只会返回一个给定的错误文档 let l = console.log let http = ajanuw.create({ uri: 'http://localhost ...
- echarts tooltip 自定义提示信息添加圆点
tooltip自定义时,给文字前加圆点 tooltip: { formatter: '{b}<br /><span style="display:inline-block; ...
- DSO论文解读
dso 1.1. Motivation 本文提出的单目视觉测距法的直接和稀疏公式是出于以下考虑因素; (1)直接: 关键点的主要优点之一是它们能够为使用现成的商品相机拍摄的图像中存在的光度和几何失真提 ...
- sqlServer自动代码提示功能
第一种方法: 首先确认 自己的sqlServer2008自身的提示功能有没有被屏蔽了! 打开sqlserver2008 -- 工具 -- 选项 -- 文本编辑器 -- 所有语言 -- 勾选 语句结束 ...
- update set from 语句用法
关键字: update set from 下面是这样一个例子: 两个表a.b,想使b中的memo字段值等于a表中对应id的name值 表a:id, name 1 ...
- 牛客多校10 D Rikka with Prefix Sum 不是数据结构
https://www.nowcoder.com/acm/contest/148/D 题意: 1e5个数,1e5个操作,操作分为: 1.区间加. 2.整个数列替换为前缀和. 3.区间查询. 查询数小于 ...
- vue中的provide/inject的学习
在 Vue.js 的 2.2.0+ 版本中添加加了 provide 和 inject 选项.用于父级组件向下传递数据.provide/inject:简单的来说就是在父组件(或者曾祖父组件)中通过pro ...
- MAC apache服务器搭建
一.启动原本服务器 首先打开“终端(terminal)”,输入 sudo apachectl -v,(可能需要输入机器秘密).如下显示Apache的版本: 可以输入启动命令进行启动: sudo apa ...
- 中文乱码与字体库windows
设置中文后,查看图形有乱码,文字变成了方块 Win+R打开运行,输入fonts,回车进入Windows字体目录,找到微软雅黑-常规字体,复制出来将文件名修改为msyh.ttf,注意后缀ttf. 将ms ...
- LeetCode 700 Search in a Binary Search Tree 解题报告
题目要求 Given the root node of a binary search tree (BST) and a value. You need to find the node in the ...