633. Sum of Square Numbers

来源:互联网 发布:PHP zend val 编辑:程序博客网 时间:2024/06/05 21:02

Given a non-negative integer c, your task is to decide whether there’re two integers a and b such that a2 + b2 = c.

Example 1:

Input: 5Output: TrueExplanation: 1 * 1 + 2 * 2 = 5

Example 2:

Input: 3Output: False
class Solution {public:    bool judgeSquareSum(int c) {        int r = sqrt(c);        int l = 0;        while(l <= r){            int sum = l * l + r * r;            if(sum == c) return true;            else if(sum > c) --r;            else ++l;        }        return false;    }};
原创粉丝点击