Untitled

 avatar
unknown
plain_text
a year ago
580 B
3
Indexable
class Solution {
public:
    bool isPerfectSquare(int num){
        long long root = static_cast<int>(sqrt(num));
        return root * root == num;
    }
    bool judgeSquareSum(int c) {
        if (isPerfectSquare(c)) return true;
        long long r = static_cast<int>(sqrt(c));
        //static_cast<int> is used for type safety and clarity
        long long l = 0;
        while(l <= r){
            long long sum = pow(l, 2) + pow(r, 2);
            if (sum == c) return true;
            else if (sum < c) ++l;
            else --r;
        }
        return false;
    }
};
Editor is loading...
Leave a Comment