当前位置:网站首页>[binary search - simple] 69 Square root of X
[binary search - simple] 69 Square root of X
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 arithmetic square root of is 2.82842…, Because the return type is an integer , The decimal part will be removed .
Tips :
0 <= x <= 231 - 1
【 Code 】
Execution time :44 ms, In all Python3 Defeated in submission 57.03% Users of
Memory consumption :15 MB, In all Python3 Defeated in submission 15.94% 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:
left=mid+1
else:
right=mid-1
return right
【 Method 2】
Execution time :36 ms, In all Python3 Defeated in submission 90.57% Users of
Memory consumption :14.8 MB, In all Python3 Defeated in submission 78.58% 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
【 Method 3】 Newton's iteration
Execution time :36 ms, In all Python3 Defeated in submission 90.57% Users of
Memory consumption :15 MB, In all Python3 Defeated in submission 18.06% Users of
Pass the test case :1017 / 1017
class Solution:
def mySqrt(self, x: int) -> int:
left,right=0,x
def cal(n):
res=(n+x/n)/2
if res==n:
return n
else:
return cal(res)
if x==0:return x
return int(cal(x))
【 Method 4: Newton's iteration 】
Execution time :44 ms, In all Python3 Defeated in submission 57.03% Users of
Memory consumption :14.8 MB, In all Python3 Defeated in submission 84.44% Users of
Pass the test case :1017 / 1017
class Solution:
def mySqrt(self, x: int) -> int:
if x==0:return 0
cur=x
while True:
temp=(cur+x/cur)/2
if cur-temp<1e-7:
break
cur=temp
return int(cur)
版权声明
本文为[Caicai 2022]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204211507332835.html
边栏推荐
- 存储系统及存储器
- May day financial products have no income?
- 论文复现——Libra R-CNN: Towards Balanced Learning for Object Detection
- 【历史上的今天】4 月 21 日:微处理器先驱诞生;Winamp 发布;COPPA 正式生效
- [机器学习导论]——第六课——贝叶斯分类器
- elemetn表单控件---未通过校验字段提交时自动定位到该字段位置
- 融云首席科学家任杰:互联网兵无常势,但总有人正年轻
- 阿里超大规模 Flink 集群运维体系介绍
- How to call uniCloud cloud storage in Nodejs project to upload middle note files
- 数字化时代,企业运维面临现状及挑战分析解读
猜你喜欢

OpenHarmony3.1 H264视频播放之路

数字化时代,SaaS软件如何成为国产化替代的轻骑兵?

QT network and communication (TCP chat room)

存储系统及存储器
![Special test 04 · differential calculus of multivariate functions [Li Yanfang's whole class]](/img/94/cf905f5d98fb03624944f990463b0d.png)
Special test 04 · differential calculus of multivariate functions [Li Yanfang's whole class]

DABDetr论文解读+核心源码解读

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

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

Jetpack Compose使用自定义操作符实现绘制五角星效果

专题测试04·多元函数微分学【李艳芳全程班】
随机推荐
【二分搜索-中等】540. 有序数组中的单一元素
干货 | 应用打包还是测试团队老大难问题?
Beautiful solution for initialization of golang Gorm framework
Excel小技巧-VLOOKUP自动匹配
SAP UI5 应用开发教程之七十 - 如何使用按钮控件触发页面路由跳转试读版
【二分查找-简单】278. 第一个错误的版本
期货开户找哪家公司?哪家期货公司最安全?
Reproduction of paper -- Libra r-cnn: rewards balanced learning for object detection
Insect 12864
&lt;译文&gt;设置Prometheus并将其与Grafana集成以进行监控
Jetpack Compose使用自定义操作符实现绘制五角星效果
SAP UI5 应用开发教程之七十 - 如何使用按钮控件触发页面路由跳转
lightGBM专题2:基于pyspark在spark平台下lightgbm训练详解
解决select2 在modal中搜索框无效的问题
【二分查找-简单】LCP 18. 早餐组合
Alibaba cloud R & D collaboration service related agreement terms | cloud efficiency
【二分搜索-中等】1498. 满足条件的子序列数目
干货 | 测试人职场晋升“潜规则”:15 年经验资深测试经理的职场忠告
Lightgbm topic 5: data consolidation of pyspark table data processing
Dry goods | record your first web automation test case