[LintCode 697] Check Sum of Square Numbers(Python)
来源:互联网 发布:使用Java无法输出数值 编辑:程序博客网 时间:2024/06/16 19:55
题目描述
Given a integer c, your task is to decide whether there’re two integers a and b such that a^2 + b^2 = c.
样例
Given n = 5
Return true // 1 * 1 + 2 * 2 = 5
Given n = -5
Return false
代码
- 法一
import mathclass Solution: """ @param: : the given number @return: whether whether there're two integers """ def checkSumOfSquareNumbers(self, num): # write your code here if num < 0: return False for i in range(int(math.sqrt(num)), -1, -1): if i * i == num: return True t = num - i * i _t = int(math.sqrt(t)) if _t * _t == t: return True return False
- 法二
import mathclass Solution: """ @param: : the given number @return: whether whether there're two integers """ def checkSumOfSquareNumbers(self, num): # write your code here if num < 0: return False a = 0 b = int(math.sqrt(num)) while a <= b: if a ^ 2 + b ^ 2 == num: return True elif a ^ 2 + b ^ 2 > num: b -= 1 else: a += 1 return False
阅读全文
0 0
- [LintCode 697] Check Sum of Square Numbers(Python)
- Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- leetcode[Sum of Square Numbers]
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers
- 633. Sum of Square Numbers。
- LeetCode 633 : Sum of Square Numbers(python)
- 【leetcode】633. Sum of Square Numbers(Python & C++)
- [leetcode]633. Sum of Square Numbers
- [LeetCode]633. Sum of Square Numbers
- LeetCode 633. Sum of Square Numbers
- [leetcode]633. Sum of Square Numbers
- leetcode 633. Sum of Square Numbers
- Sum of Square Numbers问题描述
- 633. Sum of Square Numbers标题
- 你的请帖是我的喜帖?手机管家提醒:别上当
- 每日产品辣评:价格碾压 iPhone 6s,Lumia950/950XL开启预售
- myeclipse添加rest接口
- 【重点###】SpringDataJPA的组合条件分页查询(笔记思路,便于忘了复习)页面:EasyUI
- 中兴推国民手机Blade A1 协同高端发力国内市场
- [LintCode 697] Check Sum of Square Numbers(Python)
- form表单标签
- 归心似箭,马不停蹄
- eclipse导入myeclipse web项目的方法
- 主要看气质 极致美颜手机让自拍党更大胆
- 每日产品辣评:160万元个人飞行背包深圳首秀,迪拜已预订20台
- stl list 硬粪脱水
- hive中时间日期函数的使用
- 漫步最优化三十四——高斯-牛顿法