leetcode.0633. Sum of Square Numbers
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
题目描述:判断一个数是否为两个数的平方和,例如 5 = 1^2 + 2^2。
C++
class Solution {
public:
bool judgeSquareSum(int c) {
int i = 0, j = int(sqrt(c)); /*与python区别*/
while (i<=j) {
int powSum = i*i + j*j;
if (powSum == c) {
return true;
}
else if (powSum > c) {
j--;
}
else {
i++;
}
}
return false;
}
};
Python3
class Solution:
def judgeSquareSum(self, c):
"""
:type c: int
:rtype: bool
"""
i = 0
j = int(math.sqrt(c)) #与python区别
while i<=j:
powSum = i*i + j*j
if powSum == c:
return True
elif powSum > c:
j-=1
else :
i+=1
return False