当前位置:网站首页>【LeetCode】69. x 的平方根
【LeetCode】69. x 的平方根
2022-06-12 22:18:00 【LawsonAbs】
1.题目
不准使用系统内置函数,请给出一个数的平方根。
2.思想
使用二分法。
- 注意对特殊值的处理【我在这里翻了两次车】
3.代码
class Solution:
def mySqrt(self, x: int) -> int:
low,high=0,x
# 寻找一个合适的下标,即使二者相等时,也要继续查找
while(low <= high):
mid=(low+high) //2
# print(low,high,mid)
if x == mid * mid:
return mid
if mid == 0: # 如果mid = 0,那么肯定是low=0,high = 1
low = mid+1
continue
if x // mid >= mid: # 如果
low = mid + 1
else:
high = mid - 1
# 结束while时, low > high
# 最后再判断一下
return high
边栏推荐
- Modstartcms modular station building system v3.3.0 component function upgrade, event triggering enhancement
- [machine learning] learning notes 01- introduction
- Can tonghuashun open an account? Is it safe to open an account in tonghuashun? How to open a securities account
- NoSQL - redis configuration and optimization (II) high availability, persistence and performance management
- JVM Basics - > how to troubleshoot JVM problems in your project
- JVM foundation - what is the process of loading > objects into the JVM, and then clearing them by GC?
- Why is pain rating important?
- SQL tuning guide notes 12:configuring options for optimizer statistics gathering
- JVM foundation - > three ⾊ mark
- Su embedded training day13 - file IO
猜你喜欢
SQL tuning guide notes 18:analyzing statistics using optimizer statistics Advisor
PE installation win10 system
Database daily question --- day 10: combine two tables
SQL tuning guide notes 16:managing historical optimizer statistics
Configuring Dingding notification of SQL audit platform archery
2021 rust survey results released: 9354 questionnaires collected
数据库每日一题---第10天:组合两个表
关于 安装Qt5.15.2启动QtCreator后“应用程序无法正常启动0xc0000022” 的解决方法
SQL tuning guide notes 10:optimizer statistics concepts
Cloning PDB with ADG standby
随机推荐
【Proteus仿真】简易数码管定时器时钟
Wechat applet withdrawal function
Yyds dry inventory insider news: Series high-frequency interview questions, worth a visit!
JVM Basics - > how GC determines that an object can be recycled
The interface testing tool apipos3.0 is applicable to process testing and reference parameter variables
logstash时间戳转换为unix 纳秒nano second time
About the solution to "the application cannot start normally 0xc00000022" after qt5.15.2 is installed and qtcreator is started
JVM Basics - > What are the thread shared areas in the JVM
Yyds dry goods inventory solution sword finger offer: the first non repeated character in the character stream
PCB封装下载网站推荐及其详细使用方法
USB mechanical keyboard changed to Bluetooth Keyboard
六月集训(第12天) —— 链表
How do I create a daemon thread? And where to use it?
Permission to query execution plan in Oracle Database
flutter系列之:flutter中常用的GridView layout详解
JVM foundation - > What garbage collectors does the JVM have?
List of open source alternative projects of world famous Cloud Service SaaS companies
Ansible playbook和变量(二)
SQL tuning guide notes 15:controlling the use of optimizer statistics
2022-02-28 incluxdb high availability planning