当前位置:网站首页>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;
}
};
边栏推荐
- Laravel configures passport and returns token using JWT
- Go language func function
- How does hbuilder display in columns?
- V3 02——What‘s new in Chrome extensions
- Project management - common English vocabulary I
- PS cutting height 1px, Y-axis tiling background image problem
- Problems in QT creator (additional unknown and error lines are listed in the debug output window)
- 中信期货开户麻烦吗安全吗,期货开户手续费是多少,能优惠吗
- 【BUUCTF】 EasySql
- Learn about data kinship JSON format design from sqlflow JSON format
猜你喜欢

How does hbuilder display in columns?

Shell programming overview

ES6 notes

Jetpack compose for perfect screen fit

go channel && select

Realize a simple LAN communication (similar to feiqiu)

Error on datetime when importing SQL file from MySQL

About the problems encountered when using the timer class to stop with a button (why does the QPushButton (for the first time) need to be clicked twice to respond?)

Why does the folder appear open in another program

Introduction to the construction and development of composer private warehouse
随机推荐
Impersonate server and client using message queuing
Race of golang
ot initialized – call ‘refresh’ before invoking lifecycle methods via the context: Root WebApplicati
Lfi-rce without controllable documents
XSS challenge (6-10) more detailed answers
PS tip: the video frame to Layer command cannot be completed because dynamiclink is not available
MFQE 2.0: A New Approach for Multi-FrameQuality Enhancement on Compressed Video
Knowledge learned from the water resources institute project
[extensive reading of papers] attributes guided facial image completion
ctfshow nodejs
从控制层返回到js的json数据带“\”转译符,怎么去掉
數據恢複軟件EasyRecovery15下載
Is it troublesome for CITIC futures to open an account? Is it safe? How much is the handling charge for opening an account for futures? Can you offer a discount
Detailed explanation of settimeout() and setinterval()
DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
Pit used by go language array type
Use PHP to delete the specified text content in the file
DB2 SQL Error: SQLCODE=-206, SQLSTATE=42703
Upgrade centos7 mysql5.5 to mysql5.7 non RPM in the form of tar package
Querywrapper in mybaits plus