633. 平方数之和

633. 平方数之和icon-default.png?t=M4ADhttps://leetcode.cn/problems/sum-of-square-numbers/

难度中等356

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

示例 1:

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

示例 2:

输入:c = 3
输出:false

提示:

  • 0 <= c <= 231 - 1

通过次数111,220提交次数285,934

请问您在哪类招聘中遇到此题?

class Solution {
    public boolean judgeSquareSum(int c) {
        
        long start = 0;
        long end = (long)Math.sqrt(c);
        while(start<=end)
        {
            long t = start*start+end*end;
            if(t==c) return true;
            if(t>c) end--;
            else start++;
        }

        return false;
    }
}

633. 平方数之和

 

© 版权声明
THE END
喜欢就支持一下吧
点赞9 分享
相关推荐
  • 暂无相关文章
  • 评论 抢沙发

    请登录后发表评论

      暂无评论内容