当前位置:网站首页>[binary search] 69 Square root of X
[binary search] 69 Square root of X
2022-07-05 05:16:00 【lee2813】
One 、 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 .
Example 1:
Input :x = 4
Output :2
Example 2:
Input :x = 8
Output :2
explain :8 The arithmetic square root of is 2.82842…, Because the return type is an integer , The decimal part will be removed .
Two 、 Answer key
- First, the initial interval is defined as the left closed and right closed interval , Search between edges, so
l<=r, Because when the left is closed and the right is closed , It is found that only one number remains within the boundary , for example[5,5]reasonable . - Then you just need to select the values in the interval in the order of binary search to substitute them into the problem solving .
(1) Smaller than the target value , Take the right half interval
(2) It is larger than the target value , Take left half interval
(3) Equal to the target value , Return intermediate value
(4) Until you find the whole range , There is no equal value , namelyl>r, Round down according to the intention of the question , Returns a small valuer
3、 ... and 、 Code
class Solution {
public:
int mySqrt(int x) {
if(x==0) return 0;
int l = 1;
int r = x;
int mid ,sqrt;
while(l<=r){
mid = ((l+r)/ 2;
sqrt = x / mid;
if(sqrt > mid) l = mid + 1;
else if(sqrt < mid) r = mid - 1;
else return mid;
}
return r;
}
};
边栏推荐
- 嵌入式数据库开发编程(零)
- 64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
- 2022年上半年国家教师资格证考试
- Optimization scheme of win10 virtual machine cluster
- Basic knowledge points
- 3dsmax common commands
- GameObject class and transform class of unity
- Solon Logging 插件的添加器级别控制和日志器的级别控制
- xftp7与xshell7下载(官网)
- Common database statements in unity
猜你喜欢

Collapse of adjacent vertical outer margins

django连接数据库报错,这是什么原因

Leetcode word search (backtracking method)

Merge sort
![[转]: OSGI规范 深入浅出](/img/54/d73a8d3e375dfe430c2eca39617b9c.png)
[转]: OSGI规范 深入浅出

National teacher qualification examination in the first half of 2022

【Leetcode】1352. Product of the last K numbers

质量体系建设之路的分分合合

Count sort

Embedded database development programming (zero)
随机推荐
win下一键生成当日的时间戳文件
3dsmax2018 common operations and some shortcut keys of editable polygons
一个新的微型ORM开源框架
This article is good
支持多模多态 GBase 8c数据库持续创新重磅升级
Simple HelloWorld color change
Unity3d learning notes
质量体系建设之路的分分合合
Basic knowledge points
Unity and database
UE fantasy engine, project structure
Programmers' experience of delivering takeout
Lua GBK and UTF8 turn to each other
Common database statements in unity
Dotween usage records ----- appendinterval, appendcallback
Count sort
MD5 bypass
FVP和Juno平台的Memory Layout介绍
Unity synergy
How to choose a panoramic camera that suits you?