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
Example 3:
Input: c = 4
Output: true
Example 4:
Input: c = 2
Output: true
Example 5:
Input: c = 1
Output: true
My Solution
class Solution:
def judgeSquareSum(self, c: int) -> bool:
if c == 0:
return True
else:
for i in range(c):
squareB = c - i*i
if squareB < 0:
return False
elif squareB ** 0.5 == round(squareB ** 0.5):
return True
break
return False
문제 출처 : https://leetcode.com/problems/sum-of-square-numbers/
'Algorithm > Daily Coding Tests Challenge' 카테고리의 다른 글
[Leetcode] Medium : Kth Largest in an Array (0) | 2021.11.15 |
---|---|
[Leetcode] Medium : Corporate Flight Bookings (0) | 2021.11.15 |
[Leetcode] Medium : Number of Provinces (0) | 2021.11.08 |
[Leetcode] Medium : Network Delay time (0) | 2021.10.13 |
[Leetcode] Easy: Pascal's Triangle 2 (0) | 2021.10.10 |