?题目描述
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c。
示例1:
输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5
示例2:
输入: 3
输出: False
? Method : 注意精度
class Solution {
public:
// O(sqrt(c)) = O(2^16) =O(63356) =O(1)
bool judgeSquareSum(int c) {
int m = sqrt(c);
for (int a = 0; a <= m; ++a) {
int b = round(sqrt(c - a * a)); // sqrt()因为是一个浮点操作所以有精度问题,需要四舍五入
if (a*a + b*b ==c ) return true;
}
return false;
}
};