[LeetCode]#342. Power of Four
1 min readMar 30, 2020
Environment: Python 3.7
Key technique: Hash table
Problem: 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?
Analysis:
Use Hash table to solve it without loops/recursion.
Solution:
class Solution:
def isPowerOfFour(self, num):
ans=[1, 4, 16, 64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824]
return num in ans
Submitted result: