当前位置:网站首页>[binary search - simple] sword finger offer II 072 take a square root
[binary search - simple] sword finger offer II 072 take a square root
2022-04-21 15:11:00 【Caicai 2022】
subject
Example 1:
Input : x = 4
Output : 2
Example 2:
Input : x = 8
Output : 2
explain : 8 The square root of is 2.82842…, Because the decimal part will be rounded off , So back 2
Tips :
0 <= x <= 231 - 1
Code
Execution time :44 ms, In all Python3 Defeated in submission 64.22% Users of
Memory consumption :14.8 MB, In all Python3 Defeated in submission 89.31% Users of
Pass the test case :1017 / 1017
class Solution:
def mySqrt(self, x: int) -> int:
left,right=0,x
while left<=right:
mid=left+(right-left)//2
if mid*mid==x:
return mid
elif mid*mid>x:
right=mid-1
else:
left=mid+1
return right
【 Method 2】
Execution time :40 ms, In all Python3 Defeated in submission 82.25% Users of
Memory consumption :14.8 MB, In all Python3 Defeated in submission 96.10% Users of
Pass the test case :1017 / 1017
class Solution:
def mySqrt(self, x: int) -> int:
left,right,ans=0,x,-1
while left<=right:
mid=left+(right-left)//2
if mid*mid<=x:
ans=mid
left=mid+1
else:
right=mid-1
return ans
版权声明
本文为[Caicai 2022]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211507332876.html
边栏推荐
猜你喜欢

pytorch图像分类篇:pytorch官方demo实现一个分类器(LeNet)

Dabdetr paper interpretation + core source code interpretation

融云首席科学家任杰:互联网兵无常势,但总有人正年轻

Detailed explanation of basic knowledge of database 5: index and its two engines in mysql, master-slave replication and relational / non relational database

Mysql数据库(2)

基于JSP的公益网站设计与实现

Software testing (III) p51-p104 software test case methods and defects

阿里P9详解高并发,带你了解淘宝怎么抗住双11等大型秒杀活动

SAP UI5 應用開發教程之七十 - 如何使用按鈕控件觸發頁面路由跳轉試讀版

elemetn表单控件---未通过校验字段提交时自动定位到该字段位置
随机推荐
Practical application of MySQL database (I)
SAP ui5 application development tutorial 70 - how to use button controls to trigger page routing jump trial version
Jetpack Compose使用自定义操作符实现绘制五角星效果
Druid database link problem
Worm PWM
SWOOLE高性能内存数据库的使用和配置教程
MySQL 8.0.11 installation tutorial (Windows version)
Mysql8. Method for resetting initial password above 0
Detailed explanation of basic knowledge of database 5: index and its two engines in mysql, master-slave replication and relational / non relational database
SAP UI5 应用开发教程之七十 - 如何使用按钮控件触发页面路由跳转试读版
The worm inserted hill
SAP UI5 應用開發教程之七十 - 如何使用按鈕控件觸發頁面路由跳轉試讀版
数字孪生坦克作战,科技推动战场信息数据化
Mysql 执行流程
Would like to ask how to find the software registration code from the database
期货开户找哪家公司?哪家期货公司最安全?
Dry goods | environmental problems or chronic difficulties in testing? It's easy to do it in two steps
Minimum steps of manufacturing letter ectopic words - C language solution
Computer experiment in C language of Wuhan University of science and Technology (Part I, Part II and part III)
尚硅谷智慧校园 —— 6、管理员功能实现