633. 平方数之和
题目描述
解题思路
双指针。这里需要注意结束位置,因为math.sqrt© ** 2 >= c,所以结束位置可以提前,不需要从c开始
代码实现
class Solution:
def judgeSquareSum(self, c: int) -> bool:
start = 0
# end = int(math.sqrt(c))
end = math.ceil(math.sqrt(c))
while start <= end:
res = start ** 2 + end ** 2
if res == c:
return True
elif res > c:
end -= 1
else:
start += 1
return False