-
Notifications
You must be signed in to change notification settings - Fork 0
/
isPowerOfFour.py
44 lines (37 loc) · 1013 Bytes
/
isPowerOfFour.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
'''
Source : https://leetcode.com/problems/power-of-four/
Author : Yuan Wang
Date : 2018-07-18
/***************************************************************************************
*Given an integer (signed 32 bits), write a function to check whether it is a power of 4.
*
*Example:
*Given num = 16, return true. Given num = 5, return false.
*
*Follow up: Could you solve it without loops/recursion?
****************************************************************************************/
'''
#Bit manipulation using loop
def isPowerOfFourA(num):
"""
:type num: int
:rtype: bool
"""
if num <= 0:
return False
if num & (num-1) != 0:
return False
total = 0
while num > 1:
total += 1
num = num >> 1
return total & 1 == 0
#Bit manipulation without using loop
def isPowerOfFourB(num):
"""
:type num: int
:rtype: bool
"""
return num != 0 and num &(num-1) == 0 and num & 1431655765== num
#return num > 0 and (num & (num - 1)) == 0 and (num - 1) % 3 == 0
print(isPowerOfFourA(16))