一道水水的中等题,五分钟吧
//左右两端,双指针和改成双指针平方和
//优化左右边界,右边界不会超过开平方
//注意超int范围
class Solution {
public:
bool judgeSquareSum(int c) {
int left = 0,right = sqrt(c) + 1;
while( left <= right ){
long long int sum = (long long )left*left + (long long)right*right;
if (sum == (long long )c)
return true;
else if (sum <(long long) c)
++left;
else
--right;
}
return false;
}
};