633. 平方数之和

给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c。

示例1:

输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5
 

示例2:

输入: 3
输出: False

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/sum-of-square-numbers

思路:

老规矩,双指针的题,定两个指针,给定了一个整数C,脑子里面浮现出一个数组,这里关键是数组结束的位置,开始还是0,结束位置定为c的平方根,如果可以,第一次就找到了,就是0^2+x^2,x就是这个数的平方根直接找到,nice啊马飞

class Solution {
    public boolean judgeSquareSum(int c) {
        //定义一个左指针
        int left = 0;
        //定义一个右指针
        int right = (int) Math.sqrt(c);
        boolean flag = false;
        while (left <= right) {
            if (left * left + right * right < c) {
                left++;
            } else if (left * left + right * right > c) {
                right--;
            }else {
                flag = true;
                break;
            }
        }
        return left <= right;
    }
}
原文地址:https://www.cnblogs.com/zzxisgod/p/13331546.html