0633-sum-of-square-numbers¶
Try it on leetcode
Description¶
Given a non-negative integer c, decide whether there're two integers a and b such that a2 + b2 = c.
Example 1:
Input: c = 5 Output: true Explanation: 1 * 1 + 2 * 2 = 5
Example 2:
Input: c = 3 Output: false
Constraints:
0 <= c <= 231 - 1
Solution(Python)¶
class Solution(object):
def judgeSquareSum(self, c):
def binary_search(s, e, n):
if s > e:
return False
mid = s + (e - s) // 2
if mid * mid == n:
return True
elif mid * mid > n:
return binary_search(s, mid - 1, n)
else:
return binary_search(mid + 1, e, n)
for a in range(
int(c**0.5) + 1
): # equivalent to `for (long a = 0; a * a <= c; a++)` in Java
b = c - a * a
if binary_search(0, b, b):
return True
return False