题目:给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c。
示例1:
输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5
示例2:
输入: 3
输出: False
思路:双指针
class Solution {
public boolean judgeSquareSum(int c) {
int left = 0;
int right = (int)Math.sqrt(c);
while(left<=right){
int a = (int)Math.pow(left,2);
int b= (int)Math.pow(right,2);
if((a+b) == c){
return true;
}
if((a+b)>c){
right --;
}
if((a+b)<c){
left ++;
}
}
return false;
}
}