Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit9eed79a

Browse files
Add C++ implementation
Signed-off-by: begeekmyfriend <begeekmyfriend@gmail.com>
1 parente52ea62 commit9eed79a

File tree

2 files changed

+36
-7
lines changed

2 files changed

+36
-7
lines changed

‎0069_sqrt/sqrt.c‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,26 +51,26 @@ static double mySqrt(double n)
5151
}
5252
#endif
5353

54-
staticintmySqrt(intx)
54+
intmySqrt(intx)
5555
{
5656
if (x==0) {
5757
return0;
5858
}
5959

60-
unsignedintleft=1;
61-
unsignedintright= (unsignedint)x;
62-
unsignedintmid=left+ (right-left) /2;
60+
unsignedintlo=1;
61+
unsignedinthi= (unsignedint)x;
62+
unsignedintmid=lo+ (hi-lo) /2;
6363
for (; ;) {
6464
if (mid>x/mid) {
65-
right=mid;
65+
hi=mid;
6666
}else {
6767
if (mid+1>x/(mid+1)) {
6868
break;
6969
}else {
70-
left=mid;
70+
lo=mid;
7171
}
7272
}
73-
mid=left+ (right-left) /2;
73+
mid=lo+ (hi-lo) /2;
7474
}
7575

7676
returnmid;

‎0069_sqrt/sqrt.cc‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include<bits/stdc++.h>
2+
3+
usingnamespacestd;
4+
5+
classSolution {
6+
public:
7+
intmySqrt(int x) {
8+
if (x ==0) {
9+
return0;
10+
}
11+
12+
unsignedint lo =1, hi = x;
13+
unsignedint mid = (lo + hi) /2;
14+
for (; ;) {
15+
if (mid > x / mid) {
16+
hi = mid;
17+
}else {
18+
if (mid +1 > x / (mid +1)) {
19+
break;
20+
}else {
21+
lo = mid;
22+
}
23+
}
24+
mid = (lo + hi) /2;
25+
}
26+
27+
return mid;
28+
}
29+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp