平方数之和
问题描述:给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a^2 + b^2 = c。
示例1:
输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5
示例2:
输入: 3
输出: False
public boolean judgeSquareSum(int c) {
if(c==0){
return true;
}
int a = 0;
int b = (int)Math.sqrt(1.0*c);
while(a<=b){
int C = a*a + b*b;
if(c == C){
return true;
}else if(c >C){
a++;
}else{
assert c<C;
b--;
}
}
return false;
}
参考:

浙公网安备 33010602011771号