难度: Easy
原题连接
内容描述
Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c.
Example 1:
Input: 5
Output: True
Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: 3
Output: False
思路 1 - 时间复杂度: O(lgN)- 空间复杂度: O(1)******
from math import sqrt
class Solution(object):
def judgeSquareSum(self, c):
"""
:type c: int
:rtype: bool
"""
a = 0
while a * a <= c:
if int(sqrt(c - a * a)) == sqrt(c - a * a):
return True
a += 1
return False