其他分享
首页 > 其他分享> > 633. Sum of Square Numbers

633. Sum of Square Numbers

作者:互联网

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: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5

 

Example 2:

Input: 3
Output: False

 

Approach #1: Math. [Java]

class Solution {
    public boolean judgeSquareSum(int c) {
        if (c < 0) return false;
        int l = 0, r = (int)Math.sqrt(c);
        while (l <= r) {
            int temp = l * l + r * r;
            if (temp < c) l++;
            else if (temp > c) r--;
            else return true;
        }
        return false;
    }
}

  

Reference:

https://leetcode.com/problems/sum-of-square-numbers/discuss/104930/Java-Two-Pointers-Solution

 

标签:633,Java,int,Sum,Solution,Numbers,Input,return,Math
来源: https://www.cnblogs.com/ruruozhenhao/p/10864691.html