Implement int sqrt(int x)
.
Compute and return the square root of x.
class Solution {
public:
int sqrt(int x)
{
int i=0;
int step=1;
while(step>0)
{
while((i+step)<=x/(i+step))
{
i=i+step;
step=(step<<1);
}
step=(step>>1);
}
return i;
}
};
public:
int sqrt(int x)
{
int i=0;
int step=1;
while(step>0)
{
while((i+step)<=x/(i+step))
{
i=i+step;
step=(step<<1);
}
step=(step>>1);
}
return i;
}
};