[342] Power of Four
https://leetcode.com/problems/power-of-four/description/
- algorithms
- Easy (39.71%)
- Source Code: 342.power-of-four.py
- Total Accepted: 110K
- Total Submissions: 274.2K
- Testcase Example: '16'
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?
python
class Solution(object):
def isPowerOfFour(self, num):
"""
:type num: int
:rtype: bool
"""
if num <= 0: return False
if num & num - 1: return False
while num > 1:
num = num >> 2
if not num == 1: return False
return True
#
#
# s = Solution()
# print s.isPowerOfFour(3)
# print s.isPowerOfFour(8)