Skip to content

Commit 72fede0

Browse files
committed
3
1 parent 2837b65 commit 72fede0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Sqrt(x)/Sqrt(x).cpp

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int sqrt(int x) {
4+
// Start typing your C/C++ solution below
5+
// DO NOT write int main() function
6+
long long lf = 0;
7+
long long rt = x;
8+
while (lf <= rt) {
9+
long long m = (lf + rt) >> 1;
10+
long long sq = m * m;
11+
if (sq == (long long)x)
12+
return m;
13+
else if (sq < (long long)x)
14+
lf = m + 1;
15+
else
16+
rt = m - 1;
17+
}
18+
return (int)rt;
19+
}
20+
};

0 commit comments

Comments
 (0)