当前位置:网站首页>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;
}
}
边栏推荐
- QT5 之信号与槽机制(信号与槽的基本介绍)
- [fxcg] today's market analysis
- Relation and operation of ORM table
- Oracle concept II
- Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
- Extract field year / quarter effect based on date
- Synchronization mechanism of dual namenodes
- 关于VS2019C#如何建立登陆界面输入的用户名和密码需与Access数据库的记录相匹配
- Pragma once Usage Summary
- Oracle概念二
猜你喜欢

Etcd visualization tool: kstone deployment (I), rapid deployment based on Helm

Kubernetes基础自学系列 | Ingress API讲解

Related configuration commands of Huawei LACP

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

郎酒两大王牌产品成都联动共振,持续带动光瓶酒消费浪潮

Data center table reports realize customized statistics, overtime leave summary record sharing
P. Simple application of a.r.a method in Siyuan (friendly testing)

Leetcode daily practice (main elements)
Yyds dry inventory brief chrome V8 engine garbage collection

Use pyinstaller to package py files into exe. Precautions and error typeerror:_ get_ sysconfigdata_ name() missing 1...‘ check_ Solutions to exists'
随机推荐
localDateTime类型的时间(2019-11-19T15:16:17) 用oracle的时间范围查询
#yyds干货盘点# 解决剑指offer:二叉树中和为某一值的路径(三)
Popularization of MCU IO port: detailed explanation of push-pull output and open drain output
LACP details
After the mobile phone, it was reported that Samsung also cut the output of TV and other home appliance product lines
Practice of constructing ten billion relationship knowledge map based on Nebula graph
Unity 阴影——阴影平坠(Shadow pancaking)
树莓派初步使用
Kubernetes basic self-study series | introduction to ingress API
Handling of difficult and miscellaneous problems during the installation and configuration of qt5.5.1 desktop version (configuring arm compilation Kit)
Hung - Mung! HDD Hangzhou station · salon hors ligne vous invite à construire l'écologie
2022年中国音频市场年度综合分析
C système de gestion de la charge de travail des enseignants en langues
Handwritten promise series - all
Etcd可视化工具:Kstone部署(一),基于Helm快速部署
10 minutes to master the installation steps of MySQL
锚文本大量丢失的问题
数组表示若干个区间的集合,请你合并所有重叠的区间,并返回 一个不重叠的区间数组,该数组需恰好覆盖输入中的所有区间 。【LeetCodeHot100】
Domain name binding dynamic IP best practices
Ping An technology's practice of migrating from Oracle to ubisql