当前位置:网站首页>二分查找2 - x的平方根
二分查找2 - x的平方根
2022-08-03 05:25:00 【花开花落夏】
x 的平方根
一 题目
给你一个非负整数 x ,计算并返回 x 的算术平方根 。由于返回类型是整数,结果只保留整数部分 ,小数部分将被舍去 。
注意:不允许使用任何内置指数函数和算符,例如 pow(x, 0.5) 或者 x ** 0.5 。
来源:leetcode官网
二 解题
非负整数x的算术平方根target,必然0<=target<=x,可以使用二分法来求target。
若mid*mid<=x,则说明target在[mid+1,right]之间,否则target在[left,mid-1]之间。
class Solution {
public int mySqrt(int x) {
int left = 0, right = x, mid;
int index = -1;
while(left<=right){
mid=(left+right)/2;
if((long)mid*mid<=x){
index=mid;
left=mid+1;
}else{
right=mid-1;
}
}
return index;
}
}

边栏推荐
- 网络间通信
- VS2022 encapsulation under Windows dynamic library and dynamic library calls
- 常见的电容器有哪些?唯样商城
- 自监督论文阅读笔记: MoCoV2使用动量对比学习改进基线
- 【第三周】ResNet+ResNeXt
- 什么是参数化设计,通过实操了解一下? | SOLIDWORKS 操作视频
- 观看华为AI技术领域课程--深度学习前三章总结
- 自监督论文阅读笔记FIAD net: a Fast SAR ship detection network based on feature integration attention and self
- MATLAB给多组条形图添加误差棒
- ZEMAX | 在 OpticStudio 中使用自由曲面进行设计
猜你喜欢

自监督论文阅读笔记 Multi-motion and Appearance Self-Supervised Moving Object Detection

MySql的Sql语句的练习(试试你能写出来几道呢)

交叉熵(第六周)

自监督论文阅读笔记SELF-SUPERVISED SPECTRAL MATCHING NETWORK FOR HYPERSPECTRAL TARGET DETECTION

9. Please introduce the class loading process, what is the parent delegation model?

自监督论文阅读笔记DisCo: Remedy Self-supervised Learning on Lightweight Models with Distilled Contrastive

MySql【后面附有练习题】

中空编码器的作用——唯样商城

Dynamic adjustment of web theme (2) Extraction

自监督论文阅读笔记 Self-supervised Label Augmentation via Input Transformations
随机推荐
window下VS2022封装静态库以及调用静态库
VSCODE 常见问题
自监督论文阅读笔记 Self-Supervised Visual Representation Learning with Semantic Grouping
Delightful Nuxt3 Tutorial (2): Build a Blog Quickly and Easily
自监督论文阅读笔记 Multi-motion and Appearance Self-Supervised Moving Object Detection
window下VS2022封装动态库以及调用动态库
C# 数组之回溯法
什么是参数化设计,通过实操了解一下? | SOLIDWORKS 操作视频
ZEMAX | 如何创建简单的非序列系统
二阶段提问总结
寄存器常见指令
八、抽象类的接口的区别
【第四周】MobileNet和HybridSN
MySql的Sql语句的练习(试试你能写出来几道呢)
常见的电子元器件分类介绍
IPC 通信 - IPC
STM32启动文件的选择
ZEMAX | 在 OpticStudio 中使用自由曲面进行设计
classpath:与classpath*的比较
Automatic ticket issuance based on direct reduction of China Southern Airlines app