当前位置:网站首页>【二分查找-简单】剑指 Offer II 072. 求平方根
【二分查找-简单】剑指 Offer II 072. 求平方根
2022-04-21 15:08:00 【菜菜2022】
题目
示例 1:
输入: x = 4
输出: 2
示例 2:
输入: x = 8
输出: 2
解释: 8 的平方根是 2.82842…,由于小数部分将被舍去,所以返回 2
提示:
0 <= x <= 231 - 1
代码
执行用时:44 ms, 在所有 Python3 提交中击败了64.22% 的用户
内存消耗:14.8 MB, 在所有 Python3 提交中击败了89.31% 的用户
通过测试用例: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
【方法2】
执行用时:40 ms, 在所有 Python3 提交中击败了82.25% 的用户
内存消耗:14.8 MB, 在所有 Python3 提交中击败了96.10% 的用户
通过测试用例: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
版权声明
本文为[菜菜2022]所创,转载请带上原文链接,感谢
https://blog.csdn.net/kz_java/article/details/124270864
边栏推荐
- SWOOLE高性能内存数据库的使用和配置教程
- 数字化时代,SaaS软件如何成为国产化替代的轻骑兵?
- LNK2001 - unresolved external symbol in PCL test program
- 数字孪生坦克作战,科技推动战场信息数据化
- Storage system and memory
- Computer experiment in C language of Wuhan University of science and Technology (Part I, Part II and part III)
- 亚马逊测评自养号,卖家想要获得review应该怎么做?
- 技术分享 | 黑盒测试方法论—边界值
- lightGBM专题5:pyspark表数据处理之数据合并
- lightGBM专题3:PySpark中的StringIndexer和pipeline功能实现
猜你喜欢

论文复现——Libra R-CNN: Towards Balanced Learning for Object Detection
![[today in history] April 21: microprocessor pioneer was born; Winamp release; Coppa comes into force](/img/71/6f61fb0d0a46a25d92bdb48d02f413.png)
[today in history] April 21: microprocessor pioneer was born; Winamp release; Coppa comes into force

专题测试04·多元函数微分学【李艳芳全程班】

你真的会用`timescale吗?

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

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

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

Detailed explanation of spark SQL underlying execution process

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

MySQL 8.0.11 installation tutorial (Windows version)
随机推荐
Ali's monthly salary is 15K. The interview is so simple
Mysql数据库(3)
MySQL 8.0.11 installation tutorial (Windows version)
Translation of mastering ABP framework
Improvement of ref and struct in C 11
Insect makefile
期货开户找哪家公司?哪家期货公司最安全?
手动调整slf4j的日志等级
SAP UI5 应用开发教程之七十 - 如何使用按钮控件触发页面路由跳转试读版
你真的会用`timescale吗?
专题测试04·多元函数微分学【李艳芳全程班】
【云驻共创】华为云数据库-基础知识
干货 | 应用打包还是测试团队老大难问题?
Golang Gorm框架初始化的优美解决方案
阿里超大规模 Flink 集群运维体系介绍
The worm inserted hill
MYSQL 第1章 数据库简介
&lt;译文&gt;设置Prometheus并将其与Grafana集成以进行监控
制造字母异位词的最小步骤数-c语言解决
Mysql database (3)