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;
    }
}

 

posted @ 2019-08-29 20:49  星辰大海。  阅读(267)  评论(0编辑  收藏  举报