C++ solution with binary search


  • 0
    J
    bool isPerfectSquare(int num) {
        int l = 0, r = num;
        while(l <= r) {
            long long mid = (l + r) / 2;
            if(mid * mid < num)
                l = mid + 1;
            else if(mid * mid > num)
                r = mid - 1;
            else
                return true;
        }
        
        return false;
    }

Log in to reply
 

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