Check Sum of Square Numbers
Given a integer c
, your task is to decide whether there're two integers a and b such that a^2 + b^2 = c
.
您在真实的面试中是否遇到过这个题?
Yes
样例
Given n = 5
Return true
// 1 * 1 + 2 * 2 = 5
Given n = -5
Return false
代码
class Solution {
public:
/*
* @param : the given number
* @return: whether whether there're two integers
*/
bool checkSumOfSquareNumbers(int num) {
// write your code here
if (num < ) {
return false;
}
int c = floor(sqrt(num));
int i = c;
if (i * i == num) return true;
int j = ;
while (j <= i) {
if (i * i + j * j == num) {
return true;
} else if (i * i + j * j < num) {
j++;
} else {
i--;
}
}
return false;
}
};