sum of squared numbers
This commit is contained in:
24
python/problem_633/README.md
Normal file
24
python/problem_633/README.md
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# 633. Sum of Square Numbers
|
||||||
|
|
||||||
|
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
|
||||||
|
|
||||||
|
|
||||||
25
python/problem_633/solution1.py
Normal file
25
python/problem_633/solution1.py
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
class Solution:
|
||||||
|
def judgeSquareSum(self, c: int) -> bool:
|
||||||
|
# get the square root
|
||||||
|
# int num
|
||||||
|
|
||||||
|
sqrt_num = c ** 0.5
|
||||||
|
|
||||||
|
int_num = int(str(sqrt_num).split(".")[0])
|
||||||
|
sqr_num = int_num ** 2
|
||||||
|
|
||||||
|
if sqr_num == c:
|
||||||
|
return True
|
||||||
|
|
||||||
|
# memoization
|
||||||
|
mem = set()
|
||||||
|
|
||||||
|
for i in range(int_num, 0, -1):
|
||||||
|
mem.add(i ** 2)
|
||||||
|
|
||||||
|
for i in mem:
|
||||||
|
if (c - i) in mem:
|
||||||
|
return True
|
||||||
|
|
||||||
|
return False
|
||||||
|
|
||||||
Reference in New Issue
Block a user