Skip to content

Latest commit

 

History

History
41 lines (27 loc) · 1.31 KB

File metadata and controls

41 lines (27 loc) · 1.31 KB

😋 LeetCode 263. Ugly Number

  • Date : 2021.08.29(일)
  • Time : 25분

Problem

  • An ugly number is a positive integer whose prime factors are limited to 2, 3, and 5. Given an integer n, return true if n is an ugly number.

Constraints

  • -2^31 <= n <= 2^31 - 1

Example

  • Input: n = 14
  • Output: false
  • Explanation: 14 is not ugly since it includes the prime factor 7.



풀이

    def isUgly(self, n: int) -> bool:
        if not n:
            return None

        factors = [2,3,5]

        for factor in factors:
            while n%factor == 0:
                n //= factor
            if n == 1:
                return True
        return False
        

: n을 소인수분해해서 2,3,5의 숫자가 아닌 숫자로 소인수분해가 된다면 false, 2,3,5로 나눠진다면 true을 반환하는 문제. 소수를 2,3,5로 제한한다는게 포인트이다. 나는 2,3,5를 빼고 구하라는 줄 알고 조금 오래걸렸다. 그래서 n을 2,3,5로 나눠보아서 나눠 떨어진다면 pass 하고 결국 나눠서 1까지 나온다면 소인수분해가 완료된 것 이므로 true를 반환한다. 하지만 for문을 모두 완료했지만 n이 1이 아니라는 뜻은 [2,3,5]로 소인수분해가 완벽하게 끝내지 않았다는 뜻이므로 False를 반환한다.