633.Sum of Square Numbers

633.Sum of Square Numbers

难度:Easy

Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.

Example 1:

Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
 

Example 2:

Input: 3
Output: False

完全平方数,直接遍历一遍,判断差是否是完全平方。

class Solution {
bool isSquare(int n)
{
    int tmp=sqrt(n);
    return tmp*tmp == n;
}
public:
    bool judgeSquareSum(int c) {
        for(int i=0;i<=sqrt(c);i++)
        {
            if(isSquare(c-i*i) ) return true;
        }
        return false;
    }
};

执行用时 :8 ms, 在所有 C++ 提交中击败了56.27%的用户 内存消耗 :8.1 MB, 在所有 C++ 提交中击败了76.25%的用户

Last updated