633. Sum of square numbers

Sum of Square Numbers - LeetCode

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

Example 1: Input: c = 5 Output: true Explanation: 1 * 1 + 2 * 2 = 5 Example 2: Input: c = 3 Output: false


  • code
class Solution:
    def judgeSquareSum(self, c: int) -> bool:
        l, r = 0, int(c ** 0.5)
        
        while l <= r:
            res = l ** 2 + r ** 2
            if res < c:
                l += 1
            elif res > c:
                r -= 1
            else:
                return True
            
        return False