当前位置:网站首页>Square root of X
Square root of X
2022-07-03 04:12:00 【Xiao Qi,.】
A nonnegative integer x, Calculate and return x The square root of .
Rounding off the decimal part of the result , Keep only integer parts and output .
for example :
Input :x=4
Output :2
Input :x=8
Output 2
Ideas : Use binary search to narrow the range until the final value is found and output
int mySqrt(int x){
int right=x;
int left=0;
int ans=0;
while(left<=right){
int mid=left+(right-left)/2; // Two points search
if ((double)mid * mid <= x) {
ans = mid;
left = mid + 1;// Update left border
} else {
right = mid - 1;// Update right border
}
}
return ans;
}
边栏推荐
- CVPR 2022 | 大連理工提出自校准照明框架,用於現實場景的微光圖像增强
- [set theory] set concept and relationship (set family | set family examples | multiple sets)
- 2022 beautician (intermediate) new version test questions and beautician (intermediate) certificate examination
- leetcode:297. 二叉树的序列化与反序列化
- Arduino application development - LCD display GIF dynamic diagram
- [mathematical logic] predicate logic (predicate logic basic equivalent | eliminate quantifier equivalent | quantifier negative equivalent | quantifier scope contraction expansion equivalent | quantifi
- Application of I2C protocol of STM32F103 (read and write EEPROM)
- pytorch项目怎么跑?
- 竞品分析撰写
- [Blue Bridge Road - bug free code] pcf8591 - code analysis of AD conversion
猜你喜欢
Five elements of user experience
【毕业季·进击的技术er】职场人的自白
Competitive product analysis and writing
Makefile demo
Pdf editing tool movavi pdfchef 2022 direct download
300+ documents! This article explains the latest progress of multimodal learning based on transformer
JS realizes lazy loading of pictures
CVPR 2022 | Dalian Institute of technology proposes a self calibration lighting framework for low light level image enhancement of real scenes
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
"Final review" 16/32-bit microprocessor (8086) basic register
随机推荐
Basic MySQL operations
CVPR 2022 | 大连理工提出自校准照明框架,用于现实场景的微光图像增强
2022 tea master (primary) examination questions and tea master (primary) examination question bank
Interaction free shell programming
[Apple Push] IMessage group sending condition document (push certificate) development tool pushnotification
Interface embedded in golang struct
Idea shortcut keys
Five elements of user experience
Mutex and rwmutex in golang
2022 polymerization process examination questions and polymerization process examination skills
Write it down once Net travel management background CPU Explosion Analysis
How does the pytorch project run?
JS realizes lazy loading of pictures
Is pytorch open source?
[brush questions] most elements (super water king problem)
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
【毕业季·进击的技术er】职场人的自白
Daily question - ugly number
How to download pytorch? Where can I download pytorch?
What can learning pytorch do?