https://leetcode.com/problems/sum-of-square-numbers/description/
bool judgeSquareSum(int c) {
int a = sqrt((double) c / 2.0);
int b = a;
int c2 = sqrt(c);
while(a>=0 && b<=c2) {
int s = a*a + b*b;
if(s<c) b++;
else if(s>c) a--;
else return true;
}
return false;
}
No comments:
Post a Comment