Power of Four
문제 설명
Given an integer n, return true if it is a power of four. Otherwise, return false.
An integer n is a power of four, if there exists an integer x such that n == 4x.
제한 사항
- -231 <= n <= 231 - 1
입출력 예
Example 1:
Input: n = 16 Output: true
Example 2:
Input: n = 5 Output: false
Example 3:
Input: n = 1 Output: true
Python 코드
Python code
class Solution: def isPowerOfFour(self, n: int) -> bool: return math.log(n, 1/4) % 1 == 0 if n > 0 else False
* 참고 링크 : https://leetcode.com/problems/power-of-four/discuss/637372/Python-3-Single-line
- math.log( ) 함수 이용 // loops(반복문) 또는 recursion(재귀)를 피하는 방법 중 하나// x : 필수의 로그를 계산할 값을 지정// base : 선택적으로 사용할 로그 베이스 (Default 값 : e)
- ( 값이 0 또는 음수이면 ValueError 반환, 값이 숫자가 아니면 TypeError를 반환)
- math.log(x, base)
C++ 코드
C ++ code
class Solution { public: bool isPowerOfFour(int num) { return num > 0 && (num & (num - 1)) == 0 && (num - 1) % 3 == 0; } };
출처
'코딩테스트 > Leet Code' 카테고리의 다른 글
[Leet Code SQL] 184. Department Highest Salary (0) | 2022.02.11 |
---|---|
[Leet Code] 762. Prime Number of Set Bits in Binary Representation (0) | 2022.02.11 |
[Leet Code] 622. Design Circular Queue (0) | 2022.01.21 |
[Leet Code] 232. Implement Queue using Stacks (0) | 2022.01.21 |
[Leet Code] 225. Implement Stack using Queues (0) | 2022.01.21 |