当前位置:网站首页>[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;
}
};
边栏推荐
- 2022上半年全国教师资格证下
- Judge the position of the monster in the role under unity3d
- Do a small pressure test with JMeter tool
- Solon Auth 认证框架使用演示(更简单的认证框架)
- Data is stored in the form of table
- [leetcode] integer inversion [7]
- 2022/7/1 learning summary
- 使用命令符关闭笔记本自带键盘命令
- 2022 / 7 / 1 Résumé de l'étude
- UE4/UE5 虚幻引擎,材质篇,纹理,Compression and Memory压缩和内存
猜你喜欢

Leetcode word search (backtracking method)

对象的序列化

Optimization scheme of win10 virtual machine cluster

PostgreSQL surpasses mysql, and the salary of "the best programming language in the world" is low

Count sort
![[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research](/img/17/db8614b177f33ee4f67b7d65a8430f.png)
[paper notes] multi goal reinforcement learning: challenging robotics environments and request for research

Unity check whether the two objects have obstacles by ray

C language Essay 1

Ue4/ue5 illusory engine, material chapter, texture, compression and memory compression and memory

Collapse of adjacent vertical outer margins
随机推荐
[转]MySQL操作实战(三):表联结
GameObject class and transform class of unity
2022/7/1 learning summary
Collapse of adjacent vertical outer margins
Three dimensional dice realize 3D cool rotation effect (with complete source code) (with animation code)
Unity writes timetables (without UI)
被舆论盯上的蔚来,何时再次“起高楼”?
64 horses, 8 tracks, how many times does it take to find the fastest 4 horses at least
django连接数据库报错,这是什么原因
C语言杂谈1
PMP candidates, please check the precautions for PMP examination in July
Leetcode word search (backtracking method)
Solon 框架如何方便获取每个请求的响应时间?
[转]MySQL操作实战(一):关键字 & 函数
The next key of win generates the timestamp file of the current day
远程升级怕截胡?详解FOTA安全升级
Reverse one-way linked list of interview questions
room数据库的使用
Establish cloth effect in 10 seconds
National teacher qualification examination in the first half of 2022