342. Power of Four

Given an integer (signed 32 bits), write a function to check whether it is a power of 4.

Example 1:

Input: 16
Output: true

Example 2:

Input: 5
Output: false

Follow up: Could you solve it without loops/recursion?

class Solution:
    def isPowerOfFour(self, num):
        """
        :type num: int
        :rtype: bool
        """
        n = 1
        if num==1:
            return True
        while n<num:
            n *= 4
            if n == num:
                return True
        return False
posted @ 2018-10-04 11:30  bernieloveslife  阅读(89)  评论(0编辑  收藏  举报