[LeetCode]633. Sum of Square Numbers
题目描述
思路
有点类似tow sum 从两边找,low的边界是0,high的边界是输入num的平方根取整
代码
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
bool judgeSquareSum(int c) {
int high = sqrt(c), low = 0;
while (low <= high) {
int res = low * low + high * high;
if (res == c) return true;
else if (res < c) ++low;
else --high;
}
return false;
}
};
int main() {
int num;
Solution s;
while (cin >> num) {
cout << s.judgeSquareSum(num) << endl;
}
}
本文介绍了解决LeetCode上编号为633的问题——平方数之和的方法。通过双指针技巧,从两端逼近,判断一个正整数是否可以表示为两个平方数之和。算法采用C++实现,效率高且易于理解。
415

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



