平方数之和

633. 平方数之和

问题描述:给定一个非负整数 c ,你要判断是否存在两个整数 ab,使得 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;
}

参考:

posted @ 2024-02-03 23:13  行行行行星  阅读(37)  评论(0)    收藏  举报