The key of the problem is "long" type, "int" type will be TLE. The testing case is 2**31-1


  • 0
    W

    So m*m will be overflow.

    class Solution {
    public:
        bool isPerfectSquare(int num) {
            long s=1;
            while(s*s<num)s+=1;
            if(s*s==num) return true;
            else return false;
        }
    };

Log in to reply
 

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