LeetCode 342. Power of Four

Post by ailswan Dec. 13, 2023

342. Power of Four

Problem Statement

link: LeetCode.cn LeetCode

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:

Input: n = 16 Output: true

Input: n = 5 Output: false

Input: n = 1 Output: true

Solution Approach

Algorithm

Implement

    class Solution:
    def isPowerOfFour(self, n: int) -> bool:
      if n <= 0:
          return False
      while n > 1:
          if n % 4 == 0:
              n //= 4
              print(n)
          else:
              return False
      return True