标签:leetcode
https://oj.leetcode.com/problems/sqrtx/
http://blog.csdn.net/linhuanmars/article/details/20089131
public class Solution {
public int sqrt(int x) {
// Assume x >= 0
if (x == 0)
return 0;
return s(x, 0L, (long)x);
}
private int calc(int x, long low, long high)
{
// Why using long.
// is because we might calculate (MAX_VALUE / 2 + somenumber) ^ 2
if (low > high)
{
// This is the stop point.
return (int)high;
}
long mid = (low + high) / 2;
long y = mid * mid;
if (x == y)
{
return (int)mid;
}
else if (x > y)
{
return calc(x, mid + 1, high);
}
else
{
return calc(x, low, mid - 1);
}
}
}标签:leetcode
原文地址:http://7371901.blog.51cto.com/7361901/1598893