LeetCode 633 Sum of Square Numbers (Python)

Posted by 小明MaxMing on August 25, 2021

题目

Given a non-negative integer c, decide whether there’re two integers a and b such that a^2 + b^2 = c

解题思路

使用左右两个指针0和sqrt(c),求两个数的平方和,如果小于c,left+1,大于c,right+1

代码

class Solution:
    def judgeSquareSum(self, c: int) -> bool:
        left, right = 0, int(c**0.5)
        while left <= right:
            cur = left * left + right * right
            if cur < c:
                left += 1
            elif cur > c:
                right -= 1
            else:
                return True
        return False

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅