[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;
}
}