no need long, binary search solution similar to sqrt problem


  • 0
    Z

    almost same invariants as sqrt problem

    public boolean isPerfectSquare(int num) {
        if (num < 2) return true;
    
        int l = 1, u = num / 2;
        while(l < u) {
            int m = u - (u - l)/2;
            if (m > num / m) {
                u = m - 1;
            } else {
                l = m;
            }
        }
    
        return u * u == num;
    }

Log in to reply
 

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