633.平方数之和(Sum of Square Numbers)

题目描述

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

示例1:

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

示例2:

输入: 3
输出: False

解题思路

套路,双指针

public boolean judgeSquareSum(int c) {
        int i = 0, j = (int) Math.sqrt(c);
        
        while (i <= j) {
            int powSum = i * i + j * j;
            if (powSum == c)
                return true;
            if (powSum > c)
                j--;
            else
                i++;
        }
        return false;
}

猜你喜欢

转载自blog.csdn.net/regemc/article/details/80588332