C++ 0ms binary search


  • 0
     bool isPerfectSquare(int num) {
            if(num==1) return true;
            int lower=0,upper=num;
            while(lower+1<upper){
                int mid=(lower+upper)/2;
                if(mid>num/mid) upper=mid;
                else if(mid<num/mid) lower=mid;
                else return mid*mid==num;
            }
            return false;
        }
    

Log in to reply
 

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