image_2021-10-28_02-50-24.png
35.6 KB
#N69. Sqrt(x)
problem link=>https://leetcode.com/problems/sqrtx/
#solution
problem link=>https://leetcode.com/problems/sqrtx/
#solution
class Solution {
public int mySqrt(int x) {
if (x == 0) return 0;
int start = 1, end = x;
while (start < end) {
int mid = (start + end) / 2;
if (mid <= x / mid && (mid + 1) > x / (mid + 1))
return mid;
else if (mid > x / mid)
end = mid;
else
start = mid + 1;
}
return start;
}
}