C++ solution. Similar to other solutions.


  • 0
    F
    class Solution{
    public:
        bool judgeSquareSum(int c){
            for(int i = 0; i <= sqrt(c); i++){
                int sum_left = (c - i*i);
                if(sqrt(sum_left) == floor(sqrt(sum_left))){
                    for(int j = i; j <= sqrt(sum_left); j++){
                        if(i*i + j*j > c)
                        	break;
                        if(i*i + j*j == c)
                            return true;
                    }
                }
            }
            return false;
        }
    };
    

Log in to reply
 

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