Simple concise binary search solution 0 ms


  • -1
    B
    bool isPerfectSquare(int num) {
            int low = 1;
            int high = num;
            
            while(low <= high){
                long mid = (low + high)/2;
                if(mid*mid == num)return true;
                else if(mid*mid < num)low = mid+1;
                else high = mid -1;
            }
            return false;
        }

Log in to reply
 

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