blob: 13a0dc51880bf5e926702a69d24d08d5c1560a5c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
# 342. Power of Four
"""
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 == 4^x'
"""
class Solution(object):
def isPowerOfFour(self, n):
return n != 0 and n & (n - 1) == 0 and n & 1431655765 == n
if __name__ == "__main__":
obj = Solution()
print(obj.isPowerOfFour(16)) # expect: True
print(obj.isPowerOfFour(5)) # expect: False
print(obj.isPowerOfFour(1)) # expect: True
|