双指针: 判断一个非负整数是否为两个整数的平方和。(633)

Snipaste_20200703_160643.jpg
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
题目描述:判断一个非负整数是否为两个整数的平方和。

          
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
class Solution: def judgeSquareSum(self, c: int) -> bool: a=int(c**0.5) b=1 while b<=a: if a**2+b**2==c: print(a,b) print(True) return True if a**2+b**2>c: a-=1 if a**2+b**2<c: b+=1 if b>a: print(False) return False Solution().judgeSquareSum(2)

思路:采用双指针,让a=最大的数 就是目标数的开方,b=最小的1,两数平方相加大于目标数则大数减一,反之小数加一,直到相遇

(完)
小程序获取街道等其他地址信息
运用腾讯位置服务sdk处理更多地图信息
2021年终总结
2022元旦快乐
4连锤,好痛!
保本出?
vue3.2 Keepalive踩坑
vite-plugin-vue-setup-extend 插件给script标签赋值name属性,小问题花费我一天找答案
人麻了,晚上关灯吃面
A股 挺起你的脊梁!
小程序云函数运用
小程序获取手机号,openid
等待你的评论