当前位置:网站首页>Finding the root of an integer by dichotomy
Finding the root of an integer by dichotomy
2022-06-30 14:37:00 【Douglas_ LT】
A daily topic ing, Today is a day easy topic 69. Sqrt(x)
Solution 1 ( Violence law ):
class Solution {
public:
int mySqrt(int x) {
for(int i=1;i<=x/2+1;i++){
if(i<=x/i&&(i+1)>x/(i+1)){
return i;
}
}
return 0;
}
};
Solution 2 ( Dichotomy ):
class Solution {
public:
int mySqrt(int x) {
if(x<=1){
return x;
}
int left=1,right=x/2;
while(left<=right){
int mid=(right+left)/2;
if(mid>x/mid){
right=mid-1;
}
else if(mid<x/mid){
left=mid+1;
}
else{
return mid;
}
}
return right;
}
};
边栏推荐
- @Role of ResponseBody
- Details of gets, fgetc, fgets, Getc, getchar, putc, fputc, putchar, puts, fputs functions
- MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
- 2021-08-05 leetcode notes
- Add attributes to multimode
- DiceCTF - knock-knock
- MySQL back to table query optimization
- Three uses of golang underscores
- [extensive reading of papers] multimodal joint attribute prediction and value extraction for e-commerce product
- [geek challenge 2019] PHP problem solving record
猜你喜欢

August 24, 2021 deque queue and stack

Upgrade centos7 mysql5.5 to mysql5.7 non RPM in the form of tar package

Component communication mode
![【BUUCTF】[GXYCTF2019]Ping Ping Ping1](/img/dc/4d87dfb0c2fa9cd75b54e092fd3971.jpg)
【BUUCTF】[GXYCTF2019]Ping Ping Ping1

Initial attack and defense world Misc

Detailed explanation of settimeout() and setinterval()

LIS error: this configuration section cannot be used in this path

I love network security for new recruitment assessment

Introduction to the construction and development of composer private warehouse

The first dark spring cup dnuictf
随机推荐
PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available
Small exercise of process and signal
Vue returns to the previous page without refreshing the page / Vue caches the page
ctfshow nodejs
Implementation of forwarding server using IO multiplexing
How to realize selective screen recording for EV screen recording
Logiciel de récupération de données easyrecovery15 téléchargement
Alipay certificate mode payment interface
Data recovery software easyrecovery15 Download
For loop and promise to solve the problem of concurrent callback
Project management - common English vocabulary I
Initial attack and defense world Misc
數據恢複軟件EasyRecovery15下載
Detailed explanation of the first three passes of upload Labs
Shell programming overview
ThinkPHP v3.2 comment annotation injection write shell
JS time conversion standard format, timestamp conversion standard format
LeetCode_ Stack_ Medium_ 227. basic calculator II (without brackets)
ot initialized – call ‘refresh’ before invoking lifecycle methods via the context: Root WebApplicati
val_ Loss decreases first and then increases or does not decrease but only increases