1.题目描述:
判断一个非负整数是否为两个整数的平方和。
2.同Leetcode167,使用双指针来解题
import math class Solution: def judgeSquareSum(self, c: int) -> bool: if c < 0: return False #如果c不是0 start = 0 end = int(math.sqrt(c))+1 while start <= end: if start*start + end *end ==c: return True elif start*start + end *end >c: end-=1 else: start+=1 else: return False
3.需要注意的点:
1.start 的值可以从0开始
2.number可以为0,且容易遗漏number为负数的情况
3.math.sqrt(c)为非整数,要转换成整数int(math.sqrt(c))
4.while循环条件中,start和end可以相等这种情况,也容易遗漏
5.题目看似简单,容易做错,细节很容易遗漏