版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/sinat_36811967/article/details/85805434
https://leetcode-cn.com/problems/sum-of-square-numbers/
**给定一个非负整数 c ,你要判断是否存在两个整数 a 和 b,使得 a2 + b2 = c。
示例1:
输入: 5
输出: True
解释: 1 * 1 + 2 * 2 = 5
示例2:
输入: 3
输出: False**
假定a始终小于等于b,b从最大值一直减少,a从最小值一直增大,若a>b,则表明不存在这样的两个值:
class Solution:
def judgeSquareSum(self, c):
"""
:type c: int
:rtype: bool
"""
a, b = 0, 0
while b*b < c:
b += 1
while a <= b:
if a*a+b*b == c:
return True
elif a*a+b*b > c:
b -= 1
else:
a += 1
return False