- Date : 2021.08.29(일)
- Time : 15분
- Given an integer n, return true if it is a power of three. Otherwise, return false. An integer n is a power of three, if there exists an integer x such that n == 3^x.
- -2^31 <= n <= 2^31 - 1
- Input: n = 45
- Output: false
def isPowerOfThree(self, n: int) -> bool:
if n == 1: return True
elif n < 3 : return False
if n%3 != 0:
return False
else:
return self.isPowerOfThree(n//3)
: 세제곱근으로 만들 수 있는 숫자인지 판별하는 문제. 그렇기 때문에 n을 3으로 나눠서 나머지가 0으로 떨어지지 않는다면 3으로 나눠지지 않는다는 뜻이므로 False를 주고 0으로 떨어진다면 다시 재귀로 수행하였다. 이 때 n이 들어올 때 n==1이라면 3제곱근으로 모두 처리가 된 상황이라서 True로 반환하고, 3보다 작은 값 여기서는 0, 2 이라면 세제곱근으로 만들 수 없는 숫자이므로 False를 바로 반환해주었다.