美文网首页
leetcode 633. 平方数之和

leetcode 633. 平方数之和

作者: minningl | 来源:发表于2022-04-13 23:34 被阅读0次

    题目:

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

    示例 1:

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

    输入:c = 3
    输出:false

    提示:

    0 <= c <= 231 - 1

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/sum-of-square-numbers
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    Python代码

    import math
    
    class Solution(object):
    
    
        def judgeSquareSum(self, c):
            """
            :type c: int
            :rtype: bool
            """
            left = 0
            right = int(math.sqrt(c))+1
    
            while left<=right:
                ssum = left*left + right*right
                if ssum>c:
                    right -= 1
                elif ssum<c:
                    left += 1
                else:
                    return True
            return False
    

    相关文章

      网友评论

          本文标题:leetcode 633. 平方数之和

          本文链接:https://www.haomeiwen.com/subject/uwuzsrtx.html