Sqrt(x) 求一个数的开方

Implement int sqrt(int x).

Compute and return the square root of x.

x is guaranteed to be a non-negative integer.

Example:

Input: 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we want to return an integer, the decimal part will be truncated.

 class Solution {
    public int mySqrt(int x) 
    {
        int j=0;
        if(x==1)
        {
            j=1;
        }else{
             for(int i=1;i<x;i++)
             {
                if(x>=i*i && x<(i+1)*(i+1))
                {
                    j=i;
                    break;
                }
             }
        }
        return j;
    }
}

为什么代码小数值算出的结果都是对的,到 2147395600 结果就错了?

 class Solution {
    public int mySqrt(int x) 
    {
        int j=0;
        if(x==1)
        {
            j=1;
        }else{
             for(int i=1;i<x;i++)
             {
                if(x>=i*i && x<(i+1)*(i+1))
                {
                    j=i;
                    break;
                }
             }
        }
        return j;
    }
}

int有符号整数最大表示值就是2^31-1,大概就是21.47亿,再往上就溢出了。