当前位置:网站首页>69. square root of X
69. square root of X
2022-06-11 21:57:00 【Code loving students】
Title Description :
Give you a nonnegative integer x , Calculate and return x The arithmetic square root of .
Because the return type is an integer , The result is only the integral part , The decimal part will be removed .
Be careful : No built-in exponential functions and operators are allowed , for example pow(x, 0.5) perhaps x ** 0.5 .
Topic analysis :
For this problem, we need to understand a mathematical law : A positive number x The square root of is >= x / 2, With this rule, we can think of dichotomy to choose to do this problem .
1. Dichotomy
int mySqrt(int x){
int left=1;
int right=x/2+1; // Here itself can be x/2 But because of 0 Consideration of this factor , therefore right+1
while(left<=right){
int mid = left+((right-left)>>1);// This is equivalent to mid = (right+left)/2 , It is written here to avoid overflow
if(mid>x/mid){
right=mid-1;
}
else if(mid<x/mid){
left=mid+1;
}
else{
return mid; // If the conditions are met, return
}
}
return right; // The last thing to return is the value rounded to zero , Therefore return right
}2. Newton's iteration
Ideas
Newton iterative method is a method that can be used to quickly solve the zero of function .
f(y) = y^2 - C
take C Switch to Introduction value , be y Is the value .
The code is as follows :
int mySqrt(int x){
if(x==0){ // A special case
return 0;
}
double c=x;
double x0=x;
while(1){
double x1 = (x0+c/x0)*0.5;
if(fabs(x0-x1)<1e-7){ // The distance between the two is less than e*10^-7 Power exit
break;
}
x0=x1;
}
return (int)x0; // Returns the rounded value
}边栏推荐
- 超标量处理器设计 姚永斌 第2章 Cache --2.2 小节摘录
- 如何查看win系统的安装日期
- 2022-02-28(1)
- Leetcode-43- string multiplication
- Introduction à endnotex9 et instructions pour les tutoriels de base
- Latex combat notes 3- macro package and control commands
- 领先企业推进智慧财务的同款效率工具,赶快了解一下?
- 2022-02-28(2)
- R语言书籍学习03 《深入浅出R语言数据分析》-第十二章 支持向量机 第十三章 神经网络
- Redis basic data type (list)
猜你喜欢

EndnoteX9简介及基本教程使用说明

Take off efficiently! Can it be developed like this?

Leetcode-98- validate binary search tree

Matlab: 文件夹锁定问题的解决

行而不辍,未来可期|云扩科技入选上海市专精特新企业

每日一题 -- 验证回文串

Internet of things development practice 18 scenario linkage: how does an intelligent light perceive light? (I) (learning notes)

效率起飞啊!还能这样开发的?

R language book learning 03 "in simple terms R language data analysis" - Chapter 10 association rules Chapter 11 random forest

How to view the installation date of the win system
随机推荐
LabVIEW controls Arduino to realize infrared ranging (advanced chapter-6)
In the post epidemic era, how can enterprise CIOs improve enterprise production efficiency through distance
Binary search - Learning
Nmap进行主机探测出现网段IP全部存活情况分析
Internet of things development practice 18 scenario linkage: how does an intelligent light perceive light? (I) (learning notes)
[today in history] June 11: the co inventor of Monte Carlo method was born; Google launched Google Earth; Google acquires waze
Take off efficiently! Can it be developed like this?
Win10弹出USB时出现该设备正在使用的解决方法
238. product of arrays other than itself
Uncover the secret of the popular app. Why is it so black
打印机无法打印测试页是什么原因
Learning bit segment (1)
超标量处理器设计 姚永斌 第2章 Cache --2.3 小节摘录
The shortcomings of the "big model" and the strengths of the "knowledge map"
实现栈和队列
2022-02-28(1)
Building a custom CNN model: identifying covid-19
R language book learning 03 "in simple terms R language data analysis" - Chapter 10 association rules Chapter 11 random forest
The network connection is normal, but Baidu web page can not be opened and displayed. You can't access this website solution
JVM | runtime data area; Program counter (PC register);