今天我们一起挑战leetcode第633题 Sum of Square Numbers! 这题虽然难度是medium,但其实题目相对比较单纯一些,让我们一起来看看!
Given a non-negative integer c, decide whether there're two integers a and b such that a^2 + b^2 = c.
Example 1:
Input: c = 5
Output: true
Explanation: 1 * 1 + 2 * 2 = 5Example 2:
Input: c = 3
Output: falseExample 3:
Input: c = 4
Output: trueExample 4:
Input: c = 2
Output: trueExample 5:
Input: c = 1
Output: true
这题会给我们一个整数,并希望我们确认,是否能找到两个任意整数,使此二整数a和b的平方和刚好等於题目给我们的整数。譬如5 = 1^2 + 2^2,5符合此条件,因此要回传True,否则回传False。
我们可以利用sqrt来找到,此题a和b的可能最大值。通过移项,利用for回圈来遍历所有有可能的整数,并根据规则算出相对应的数,而再去利用找到的数,来做题目要求的检查,只要找到一个符合的,就直接回传True。遍历完所有的可能整数後,还没找到就回传False
以下是python的程序码
import math
class Solution:
def judgeSquareSum(self, c: int) -> bool:
m = int(math.sqrt(c)) # a或b可能的最大值
for a in range(m+1):
b = round(math.sqrt(c-a*a))
if a*a + b*b == c:
return True
return False
>>: Day7. 依点成形,创造物件 - RigidBody(中)
-什麽是管理? -目标和目的 目标设定过程可以考虑综合因素,但目标要具体,并以关键指标衡量。在不提...
渗透测试流程 与客户进行签约,取得合法的测试权限後,以下为签约与接洽需要注意: 企业是否了解渗透测...
我们之前介绍的 WordPress 原生区块有时会遇上不足够的情况,因为功能都偏向基础和简单。部分...
我真的没想到渐层可以写这麽多篇XD 本来预期是一篇就可以结束... 结果开始研究发现,不得了阿这个属...
Windows Exploit Suggester Windows Exploit Suggeste...