当前位置:网站首页>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;
}
}
边栏推荐
- LeetCode 124. Binary tree maximum path sum - binary tree series question 8
- tensorflow求解泊松方程
- Relation and operation of ORM table
- Bit.Store:熊市漫漫,稳定Staking产品或成主旋律
- Domain name binding dynamic IP best practices
- Qt5 signal and slot mechanism (demonstrate the correlation between the control's own signal and slot function)
- LeetCode每日一练(两数之和)
- C语言教师工作量管理系统
- What is RPC
- 印象深刻的问题
猜你喜欢

数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】

Source NAT address translation and server mapping web page configuration of firewall Foundation

Mihayou sued Minmetals trust, which was exposed to product thunderstorms

Sliding window + monotone queue concept and example (p1886 Logu)

Open source 23 things shardingsphere and database mesh have to say

Alibaba cloud liupeizi: Inspiration from cloud games - innovation on the end

Oracle concept II

Raspberry pie preliminary use

When the publish / subscribe mode encounters NET
锚文本大量丢失的问题
随机推荐
Awk determines whether the first column is greater than 1. If it is greater than 1, the row will be printed
Cloud security daily 220216: root privilege escalation vulnerability found on IBM SaaS integration platform needs to be upgraded as soon as possible
wheel ui
Cesium realizes satellite orbit detour
全面解析零知识证明:消解扩容难题 重新定义「隐私安全」
d3dx9_ How to repair 25.dll? d3dx9_ 25.dll where to download
Ping An technology's practice of migrating from Oracle to ubisql
事件监听机制
About how vs2019c # establishes the login interface, the user name and password entered must match the records in the access database
Yyds dry inventory solution sword finger offer: a path with a certain value in the binary tree (3)
EMQ 助力青岛研博建设智慧水务平台
Mobile terminal click penetration
Qt5 signal and slot mechanism (demonstrate the correlation between the control's own signal and slot function)
印象深刻的问题
Domain name binding dynamic IP best practices
Synchronization mechanism of dual namenodes
C language set operation
Missing d3d10 How to repair DLL files? Where can I download d3d10.dll
Mihayou sued Minmetals trust, which was exposed to product thunderstorms
Simulated process scheduling