leetcode 633.平方数之和(双指针 java)

https://leetcode-cn.com/problems/sum-of-square-numbers/submissions/

注意是a<=b,不然当c=2时发生错误,

解法同167

代码:

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

猜你喜欢

转载自www.cnblogs.com/y1040511302/p/11431948.html
今日推荐