C++ easy solution binary search runtime 2ms


  • 0
    class Solution {
    public:
        bool isPerfectSquare(int num) {
            if(num%10==2 || num%10==3 || num%10==7 || num%10==8) return false;
           int low=1;
           int high = num;
           while(low<high){
               long mid = low + (high-low)/2;
               if(mid*mid<num) low = mid+1;
               else if(mid*mid>num) high=mid;
               else return true;
           }
           return low*low==num;
        }
    };

  • 0
    L

    why mid's type is "long" rather than "int"? when change to "int" the answer is wrong.


Log in to reply
 

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