java二分法

public int mySqrt(int x) {
        long lowerBound = 0;
        long upperBound = 0;
        if (0 <= x && x < 1) {
            lowerBound = x;
            upperBound = 1;
        }
        if (x >= 1) {
            lowerBound = 1;
            upperBound = x;
        }
        long result = (lowerBound + upperBound) / 2;
        while (true) {
            if (result * result == x) {
                break;
            }
            if (result * result > x) {
                upperBound = result;
                result = (lowerBound + upperBound) / 2;
            }
            if (result * result < x) {
                if ((result + 1) * (result + 1) > x) {
                    break;
                }

                lowerBound = result;
                result = (lowerBound + upperBound) / 2;
            }
        }
        return Integer.valueOf(String.valueOf(result));
    }