[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.

思路就是二分查找法。

public int guessNumber(int n) {
        int low = 1, 
Leetcode 374.字大小 二分 阅读详情

相关推荐

【简单】力扣算法题解析LeetCode374:猜字大小

LeetCode 374题要求通过guess API在范围[1, n]内高效找出目标字。核心解法采用二分查找:算法:初始化边界left=1,right=n,循环计算中点mid并调用guess,根据返回值调整边界(-1调右,1调左,0命中),确保O(log n)时间与O(1)空间。优化:使用left + (right - left)/2防溢出,循环终止条件为left <= right。实现:Java代码通过动态调整边界快速收敛,题目保证解存在,无需额外判空。 关键词:二分查找、API交互、边界优化、整防溢

ylumwd的博客 927

LeetCode击败99%+】猜字大小

题目 猜游戏的规则如下: 每轮游戏,我都会从 1 到 n 随机选择一个字。 请你猜选出的是哪个字。 如果你猜错了,我会告诉你,你猜测的字比我选出的字是大了还是小了。 你可以通过调用一个预先定义好的接口 int guess(int num) 来获取猜测结果,返回值一共有 3 种可能的情况(-1,1 或 0): -1:我选出的字比你猜的字小 pick < num 1:我选出的字比你猜的字大 pick > num 0:我选出的字和你猜的字一样。pic

Java技术工坊 1089

leetcode374.字大小(二分查找)

详解leetcode374.字大小

2301_76197086的博客 298

LeetCode 374.字大小

🌈🌈😄😄 欢迎来到茶色岛独家岛屿,本期将为大家揭晓LeetCode 374.字大小,做好准备了么,那么开始吧。 🌲🌲🐴🐴

weixin_62275996的博客 688

[leetcode] 374. Guess Number Higher or Lower 解题报告

题目链接:https://leetcode.com/problems/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

小榕流光的专栏 3189

LeetCode //C - 374. Guess Number Higher or Lower

【代码】LeetCode //C - 374. Guess Number Higher or Lower

Made in Code 903

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 h

SparkSnail 1926

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 guess // @return -1 if my number is lower, 1 if my number is higher, otherwise return 0 int gue...

weixin_30679823的博客 109

Python [Leetcode 374]Guess Number Higher or Lower

题目描述: We are playing the Guess Game. The game is as follows: I pick a number from1ton. You have to guess which number I picked. Every time you guess wrong, I'll tell you whether the numb...

dkk37351的博客 265

LeetCode 374 Guess Number Higher or Lower

今天学长推荐LeetCode,我就挑个Easy的题目来做做,结果根本搞不清怎么输出。后来总算知道了怎么输出,然后算法算法就是个二分查找,我居然写不对!!! 好在《算法(第四版)》P241有,必须照着打才行,不能自己发挥 (l+r)/2 TLE l + (r - l) / 2 ACCode// Forward declaration of guess API. // @param

more time 312

LeetCode374 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 nu...

zhangjun62的博客 178

LeetCode374 Guess Number Higher or Lower

题目描述 We are playing the Guess Game. The game is as follows: I pick a number from1ton. You have to guess which number I picked. Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess. You call a pre...

Vergegenkunft_的博客 163

leetcode374. Guess Number Higher or Lower

二分时,记当前区间为 [left,right],初始时 left=1,right=n。记区间中间元素为 mid,若有 guess(mid)≤0 则说明 pick∈[left,mid],否则 pick∈[mid+1,right]。当区间左右端点相同时,则说明我们找到了答案,退出循环。记选出的字为 pick,猜测的字为 x。根据题目描述,若 guess(x)≤0 则说明 x≥pick,否则 x<pick。根据这一性质我们可以使用二分查找来求出答案 pick。

weixin_44245188的博客 512

[leetcode] 374. Guess Number Higher or Lower

Description 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...

Learning from the mistakes 122

leetcode[374]Guess Number Higher or Lower

输入:玩家1从1~n中选出一个,玩家2不断猜 输出:玩家1选的字target 主要思想:我的代码是基于binary search思想。 [2]中,给出了Ternary Search思想。[2]中介绍了为什么Ternary Search时间复杂度低,而binary search更广泛使用呢? input:two pivot(m1,m2) output:target target&lt...

Black Mamba的博客 232

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 h

judadeshu的博客 423

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

abpenguin的博客 370
上一篇: [LeetCode]--371. Sum of Two Integers
下一篇: [LeetCode]--383. Ransom Note
杜鲁门
博客等级 码龄11年 219粉丝 316原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值