题解 | 平方和
平方和
https://www.nowcoder.com/practice/6eade96172be4c8ba492747156481b9b
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param c int整型
# @return bool布尔型
#
class Solution:
def square(self , c: int) -> bool:
# write code here
n, res = int(c**0.5), list()#最接近的平方根,存储平方根的列表
for x in range(1,n+1):#寻找1~n的所有平方根
res += [x**2]
l, r = 0, n-1
while l<=r:#寻找能够匹配的平方和,如找到的返回True,否则末尾返回False
if res[l]+res[r]>c:
r -= 1
if res[l]+res[r]<c:
l += 1
if res[l]+res[r]==c:
return True
return False



