633. Sum of Square Numbers
Medium
1411468Add to ListShare
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
Constraints:
0 <= c <= 231 - 1
class Solution:
def judgeSquareSum(self, c: int) -> bool:
"""
a^2+b^2=c,假设b=0,a<=sqrt(c)
assert Solution().judgeSquareSum(10000000)
assert Solution().judgeSquareSum(0)
assert Solution().judgeSquareSum(1)
assert Solution().judgeSquareSum(5)
assert Solution().judgeSquareSum(4)
assert not Solution().judgeSquareSum(88)
"""
for i in range(0, int(math.sqrt(c)) + 1):
j = int(math.sqrt(c - i * i))
if i * i + j * j == c:
return True
return False