当前位置:网站首页>Leetcode skimming questions_ Sum of squares

Leetcode skimming questions_ Sum of squares

2022-07-06 01:24:00 Figure throne

Title Description

Given a nonnegative integer  c , You have to decide if there are two integers  a  and  b, bring  a2 + b2 = c .

 Java resolvent

class Solution {
    public boolean judgeSquareSum(int c) {
        long a = 0;
        int b = (int)Math.sqrt(c);
        while(a <= b)
        {
            // a * a + b * b It is possible to surpass int Value range , Need to use long
            if(a * a + b * b > c)
            {
                b--;
            }
            else if(a * a + b * b < c)
            {
                a++;
            }
            else
            {
                return true;
            }
        }
        return false;
    }
}

 C resolvent

bool judgeSquareSum(int c){
    long a = 0;
        int b = (int)sqrt(c);
        while(a <= b)
        {
            // a * a + b * b It is possible to surpass int Value range , Need to use long
            if(a * a + b * b > c)
            {
                b--;
            }
            else if(a * a + b * b < c)
            {
                a++;
            }
            else
            {
                return true;
            }
        }
        return false;
}

 

原网站

版权声明
本文为[Figure throne]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060119162698.html