Leetcode-633. 平方数之和
给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 aa + bb = c。
示例1:
输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5
示例2:
输入: 3
输出: False
思路:
双指针法。但是注意left * left == c - right * right要写成这种形式,不要写成left * left + right * right == c,可能会越界。
C++ code:
class Solution {
public:
bool judgeSquareSum(int c) {
int left = 0;
int right = sqrt(c);
while(left <= right){
if(left * left == c - right * right){
return true;
}
if(left * left < c - right * right){
left++;
}else{
right--;
}
}
return false;
}
};
本文解析LeetCode上的第633题“平方数之和”,探讨了如何判断是否存在两个整数的平方和等于给定的非负整数。采用双指针法解决此问题,并提供了C++实现代码。
589

被折叠的 条评论
为什么被折叠?



