当前位置:网站首页>[binary search - simple] 374 Guess the size of the number
[binary search - simple] 374 Guess the size of the number
2022-04-21 15:11:00 【Caicai 2022】
【 subject 】
【 Example 】
Example 1:
Input :n = 10, pick = 6
Output :6
Example 2:
Input :n = 1, pick = 1
Output :1
Example 3:
Input :n = 2, pick = 1
Output :1
Example 4:
Input :n = 2, pick = 2
Output :2
【 Tips 】
1 <= n <= 231 - 1
1 <= pick <= n
【 Code 】
Execution time :28 ms, In all Python3 Defeated in submission 96.22% Users of
Memory consumption :14.8 MB, In all Python3 Defeated in submission 74.59% Users of
Pass the test case :25 / 25
# 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: int) -> int:
class Solution:
def guessNumber(self, n: int) -> int:
low,high=1,n
while low<=high:
mid=low+(high-low)//2
if guess(mid)==0:
return mid
elif guess(mid)>0:
low=mid+1
else:
high=mid-1
return -1
版权声明
本文为[Caicai 2022]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211507333091.html
边栏推荐
- Tutoriel de développement d'applications SAP ui5 70 - Comment utiliser un contrôle de bouton pour déclencher un saut de routage de page
- 制造字母异位词的最小步骤数-c语言解决
- May day financial products have no income?
- Improvement of ref and struct in C 11
- 【云驻共创】华为云数据库-基础知识
- MySQL 8.0.11安装教程(Windows版)
- Qt网络与通信(TCP聊天室)
- Insect makefile
- EL&JSTL
- Software testing (III) p51-p104 software test case methods and defects
猜你喜欢

How to provide CPU branch prediction efficiency at the code level

Mysql8.0以上重置初始密码的方法

阿里超大规模 Flink 集群运维体系介绍

Mysql database (3)

elemetn表单控件---未通过校验字段提交时自动定位到该字段位置

Alibaba cloud R & D collaboration service related agreement terms | cloud efficiency

【天梯赛】L2-040 哲哲打游戏 (25 point(s))(模拟)

OpenHarmony3.1 H264视频播放之路

Spark/Scala - 读取 RcFile && OrcFile

Shang Silicon Valley smart campus - 6. Realization of administrator function
随机推荐
【C语言进阶】自定义类型:结构体,位段,枚举,联合
Libmysql.com in vs2019 Lib garbled code
智慧公安二维码定位报警系统开发 移动警务app
智慧党建系统开发,智慧组工党员信息管理平台建设
开发者openshift4使用入门教程 - 16 - 使用init container挂载pinpoint APM监控
【二分查找-简单】69. x 的平方根
在kubesphere上部署edgemesh的大坑
想请问一下如何从数据库中找到软件注册码
pytorch图像分类篇: 花分类数据集下载和AlexNet网络搭建训练
技术分享 | 黑盒测试方法论—边界值
MySQL 8.0.11安装教程(Windows版)
【云驻共创】华为云数据库-基础知识
C# 11 对 ref 和 struct 的改进
Worm PWM
Software testing (III) p51-p104 software test case methods and defects
C语言预处理问题
干货 | 测试人职场晋升“潜规则”:15 年经验资深测试经理的职场忠告
Mysql数据库(3)
QT network and communication (TCP chat room)
亚马逊测评自养号,卖家想要获得review应该怎么做?