给一个数字c,判断是否满足a2 + b2 = c
双指针,end最大为c开根号,beg最小为0.判断当前beg和end是否满足,然后相应移动beg或者end
乘法考虑越界问题,部分值需要 double 类型
public class Solution {
public boolean judgeSquareSum(int c) {
double beg = 0;
int end = (int) Math.sqrt(c);
while (beg <= end) {
double target = beg * beg + end * end;
if (target > c) {
end--;
} else if (target < c) {
beg++;
} else {
return true;
}
}
return false;
}
}