feat: 69_sqrt_x
This commit is contained in:
parent
6e3cf576e5
commit
a9b9675d7f
27
69_sqrt_x/main.cpp
Normal file
27
69_sqrt_x/main.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution {
|
||||||
|
public:
|
||||||
|
int mySqrt(int x) {
|
||||||
|
if (x == 1) return 1;
|
||||||
|
|
||||||
|
long long l = 0, r = x;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
long long m = (l + r) / 2;
|
||||||
|
if (m * m > x) {
|
||||||
|
r = m;
|
||||||
|
} else if (m * m < x) {
|
||||||
|
if ((m + 1) * (m + 1) > x) {
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
l = m;
|
||||||
|
} else {
|
||||||
|
return m;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user