您的位置:首页 > 其它

leetcode 69. Sqrt(x)

2016-03-14 13:52 369 查看
Implement 
int sqrt(int x)
.

Compute and return the square root of x.

int mySqrt(int x)
{
if (x == 0)
return 0;
long long int xx = x;
long long int up = xx;
long long int down = 0;
while (true)
{
if (up*up > xx)
up = (up+down)/2;
else
{
int t = down;
down = up;
up = 2 * up - t;
if (up-down==1&&up*up>=xx||up==down)
return up*up == x ? up : down;
}
}
}

accepted

类似二分查找,夹逼思想(不要想歪了)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: