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.
1
Example 1:
2
3
Input: 5
4
Output: True
5
Explanation: 1 * 1 + 2 * 2 = 5
6
7
8
Example 2:
9
10
Input: 3
11
Output: False
Copied!
完全平方数,直接遍历一遍,判断差是否是完全平方。
1
class Solution {
2
bool isSquare(int n)
3
{
4
int tmp=sqrt(n);
5
return tmp*tmp == n;
6
}
7
public:
8
bool judgeSquareSum(int c) {
9
for(int i=0;i<=sqrt(c);i++)
10
{
11
if(isSquare(c-i*i) ) return true;
12
}
13
return false;
14
}
15
};
Copied!
执行用时 :8 ms, 在所有 C++ 提交中击败了56.27%的用户 内存消耗 :8.1 MB, 在所有 C++ 提交中击败了76.25%的用户
Copy link