2021년 8월 25일 수요일

Leetcode 25h August

Leetcode 25h August

Sum of Square Numbers

이 문제는 a2 + b2 = c 가 존재하는지 체크 하는 로직입니다.

This problem is checking whether has a condition values like a formula that a2 + b2 = c

이경우 저는 c - a2 값을 저장한 DP 리스트를 만들고, for 문을 돌면서 math.sqrt가 정수인 값을 찾도록 있을 경우 True 없을 경우 False를 리턴하는 방식을 사용했습니다.

in this case, i create one list named DP, in theirs they saved c - a2 value until a * a <= c after that loop the dp and checking whether the square root of element is integer value or not

Python

class Solution:
    def judgeSquareSum(self, c: int) -> bool:
        dp = []
        i = 0
        j = 0
        while i * i <= c:
            dp.append(c - i * i)
            i += 1
        for x in dp:
            if math.sqrt(x) % 1 == 0:
                return True
        return False

댓글 없음:

댓글 쓰기