您的位置:首页 > 其它

[LeetCode]633. Sum of Square Numbers

2017-09-20 00:39 253 查看

[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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: