Clean fast java solution


  • 0
    W
    public boolean isPerfectSquare(int num) {
        if (num < 0) {
            return false;
        }
        if (num <= 1) {
            return true;
        }
        long pre = 1;
        int i = 0;
        while (i != 1) {
            for (i = 1; (pre + i) * (pre + i) <= num; i *= 2) {
                pre = i + pre;
                if (pre * pre == num) {
                    return true;
                }
            }
        }
        if (pre * pre == num) {
            return true;
        }
        return false;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.