엔지니어 게시판
LeetCode 솔루션 분류

[8/22] 342. Power of Four

컨텐츠 정보

본문

Easy
2485314Add to ListShare

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.

 

Example 1:

Input: n = 16
Output: true

Example 2:

Input: n = 5
Output: false

Example 3:

Input: n = 1
Output: true

 

Constraints:

  • -231 <= n <= 231 - 1

 

Follow up: Could you solve it without loops/recursion?
Accepted
387,825
Submissions
857,516
태그 ,

관련자료

댓글 2

학부유학생님의 댓글

  • 익명
  • 작성일
class Solution:
    def isPowerOfFour(self, n: int) -> bool:
        if n < 1: return False
        while not n%4:
            n /= 4
        
        return True if n == 1 else False

재민재민님의 댓글

  • 익명
  • 작성일
Runtime: 2 ms, faster than 60.92% of C++ online submissions for Power of Four.
Memory Usage: 5.9 MB, less than 24.97% of C++ online submissions for Power of Four.
class Solution {
public:
    bool isPowerOfFour(int n) {
        long val = 1;
        while(1) {
            if(n == val)
                return true;
            else if(val > n)
                break;
            else if(val < n)
                val *= 4;
            
        }
        
        return false;
        
    }
};
전체 396 / 5 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 223 명
  • 오늘 방문자 6,497 명
  • 어제 방문자 5,793 명
  • 최대 방문자 11,134 명
  • 전체 회원수 1,108 명
알림 0