[LeetCode]633. Sum of Square Numbers

来源:互联网 发布:在淘宝卖手机 编辑:程序博客网 时间:2024/06/05 15:32

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