当前位置:网站首页>leetcode 69. Square root of X
leetcode 69. Square root of X
2022-06-27 16:58:00 【chenyson】
difficulty : Simple
The frequency of :77
subject : Give you a nonnegative integer x , Calculate and return x Of Arithmetical square root .
Because the return type is an integer , Results are retained only Integral part , The decimal part will be Give up .
Be careful : No built-in exponential functions and operators are allowed , for example pow(x, 0.5) perhaps x ** 0.5 .

Their thinking : Two points search
- Binary search is the open root value
- Because it is possible that the open radical value is a decimal , That is, there is no equality . So you can't judge to wait first mid*mid==x
- Take out the decimal point , That is to say, the square of the calculated value is always less than the true open radical value , So please mid*mid Can only <=x
Code :
class Solution {
public int mySqrt(int x) {
int l=0,r=x,res=-1;
while(l<=r){
int mid=(l+r)/2;
// here mid*mid No parentheses
// Because only integers , That is to say, the value we want to get must be less than the true open radical
// Only when it is smaller than the true open root , Is the correct value , If it exceeds the true open radical value , That is wrong
if((long)mid*mid<=x){
// The middle value is less than X, That explains. X The open radical of is on the right
res=mid;
l=mid+1;
}else{
// It can also be on the left
r=mid-1;
}
}
return res;
}
}
边栏推荐
- d3dx9_ Where is 35.dll? d3dx9_ Where can I download 35.dll
- Domain name binding dynamic IP best practices
- A large number of missing anchor text
- tensorflow求解泊松方程
- Related configuration commands of Huawei LACP
- Redis Series 2: data persistence improves availability
- C语言教师工作量管理系统
- 全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
- Solving Poisson equation by tensorflow
- P. Simple application of a.r.a method in Siyuan (friendly testing)
猜你喜欢

What is the level 3 password complexity of ISO? How often is it replaced?

一个机器人位于一个 m x n 网格的左上角 。机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角。问总共有多少条不同的路径?【LeetCodeHot100】

IDE Eval reset unlimited trial reset

模拟进程调度

10 minutes to master the installation steps of MySQL

继手机之后 报道称三星也削减了电视等家电产品线的产量

【多线程】线程通信调度、等待集 wait() 、notify()

Redis Series 2: data persistence improves availability

Leetcode 5. Longest Palindromic Substring

When the publish / subscribe mode encounters NET
随机推荐
Oracle概念三
鴻蒙發力!HDD杭州站·線下沙龍邀您共建生態
Adaoracle supports multi chain distributed Oracle with wide area node quotation
等保三级密码复杂度是多少?多久更换一次?
LACP details
模拟进程调度
Cesium realizes satellite orbit detour
Leetcode daily practice (sum of two numbers)
10分钟掌握mysql的安装步骤
Determine the maximum number of specific words in a string
Autodesk NavisWorks 2022 software installation package download and installation tutorial
How to modify / display GPIO status through ADB shell
QT audio playback upgrade (7)
localDateTime类型的时间(2019-11-19T15:16:17) 用oracle的时间范围查询
[Niuke's questions] nowcoder claims to have remembered all Fibonacci numbers between 1 and 100000. To test him, we gave him a random number N and asked him to say the nth Fibonacci number. If the nth
Simulated process scheduling
2022年中国音频市场年度综合分析
Oracle概念二
Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie
06. First introduction to express