当前位置:网站首页>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;
}
}
边栏推荐
- What do fast fashion brands care more about?
- 印象深刻的问题
- 米哈游起诉五矿信托,后者曾被曝产品暴雷
- Oracle concept II
- Leetcode 5. Longest Palindromic Substring
- Mode setting of pulseaudio (21)
- 智慧风电 | 图扑软件数字孪生风机设备,3D 可视化智能运维
- 防火墙基础之源NAT地址转换和服务器映射web页面配置
- C language course design
- New method of cross domain image measurement style relevance: paper interpretation and code practice
猜你喜欢

字节跳动埋点数据流建设与治理实践

Mobile terminal click penetration

Four characteristics of transactions
P. Simple application of a.r.a method in Siyuan (friendly testing)

Domain name binding dynamic IP best practices

Hierarchical clustering and case analysis

LeetCode每日一练(两数之和)

d3dx9_ 39.dll how to repair -d3dx9_ 39.dll missing file download

Leetcode daily practice (main elements)

A robot is located in the upper left corner of an M x n grid. The robot can only move down or right one step at a time. The robot attempts to reach the lower right corner of the grid. How many differe
随机推荐
Leetcode 704. Binary search
米哈游起诉五矿信托,后者曾被曝产品暴雷
Domain name binding dynamic IP best practices
阿里云刘珅孜:云游戏带来的启发——端上创新
Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'
华为云首次解读云原生2.0十大典型架构,加速构建现代化应用
Leetcode daily practice (sum of two numbers)
P. Simple application of a.r.a method in Siyuan (friendly testing)
印象深刻的问题
Cloud security daily 220216: root privilege escalation vulnerability found on IBM SaaS integration platform needs to be upgraded as soon as possible
Hierarchical clustering and case analysis
Synchronization mechanism of dual namenodes
Relation and operation of ORM table
[multithreading] thread communication scheduling, waiting set wait(), notify()
Autodesk Navisworks 2022软件安装包下载及安装教程
d3dx9_ How to repair 33.dll? d3dx9_ What if 33.dll is lost?
List to table
EMQ 助力青岛研博建设智慧水务平台
Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm
Related configuration commands of Huawei LACP