当前位置:网站首页>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
}边栏推荐
- JVM | runtime data area; Program counter (PC register);
- 学习位段(1)
- C语言实现八种排序(1)
- 每日一题 - 罗马数字转整数
- 206.反转链表
- 189. rotation array
- Collection of articles and literatures related to R language (continuously updated)
- 如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称
- 每日一题-删除有序数组的重复项
- [academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?
猜你喜欢

华为设备配置H-VPN

Example of using zypper command

The network connection is normal, but Baidu web page can not be opened and displayed. You can't access this website solution

【学术相关】申请审核制下,到双一流大学读博的难度有多大?

Nmap进行主机探测出现网段IP全部存活情况分析

C语言实现迷宫问题

The shortcomings of the "big model" and the strengths of the "knowledge map"

Cdr2022 serial number coreldraw2022 green key

网络连接正常但百度网页打不开显示无法访问此网站解决方案

Two methods to judge the storage of large and small end
随机推荐
win10字体模糊怎么调节
LaTex实战笔记 3-宏包与控制命令
学习位段(1)
Nmap performs analysis of all network segment IP survivals in host detection
R language book learning 03 "in simple terms R language data analysis" - Chapter 8 logistic regression model Chapter 9 clustering model
C语言实现迷宫问题
Customer information management software
即将首发 | 业界首个零售数字化创新白皮书,解锁全链路数字化致胜秘籍
Example of using zypper command
LabVIEW Arduino electronic weighing system (project Part-1)
C语言实现八种排序 - 归并排序
RPA+低代码助推品牌电商启新创变、重启增长
Redis basic data type (Zset) ordered collection
Huawei equipment configuration h-vpn
Parker plunger pump pv180r1k1t1nmmc
Leetcode-98- validate binary search tree
如何使用事物码 SAT 查找某个 SAPGUI 屏幕字段对应的后台存储数据库表的名称
EndnoteX9簡介及基本教程使用說明
238. product of arrays other than itself
8、 BOM - chapter after class exercises and answers