LeetCode633. Sum of Square Numbers(双指针)

题意:给定一个非负整数c,确定是否存在a和b使得a*a+b*b=c。

class Solution {
typedef long long LL;
public:
    bool judgeSquareSum(int c) {
        LL head = 0;
        LL tail = (LL)(sqrt(c));
        while(head <= tail){
            LL sum = head * head + tail * tail;
            if(sum == (LL)c) return true;
            else if(sum > (LL)c) --tail;
            else ++head;
        }
        return false;
    }
};

  

posted @ 2020-02-17 19:42  Somnuspoppy  阅读(153)  评论(0编辑  收藏  举报