얼렁뚱땅 스며드는 Data Science

Algorithm/Daily Coding Tests Challenge

[Leetcode] Medium : Sum of Square Numbers

Jesip14 2021. 11. 15. 16:10

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/

 

Sum of Square Numbers - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com