Straightforward Java Solution


  • 0

    check the num from 0 to square root of c to see if the given number is Sum of Square Numbers

    public boolean judgeSquareSum(int c) {
            if(c==0) return true;
            double t = Math.sqrt((double)c);
            for(int i = 0;i<=t+1;i++){
                long cc = (long)c;
                long rest = cc - i*i;
                double d = Math.sqrt((double)rest);
                if(d%1==0){ // double num % 1 would keep the decimal part, for example 3.5d%1=0.5
                    return true;
                }
            }
            return false;
        }
    

Log in to reply
 

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