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 == 4x.
General Solution for any power
- num should be greater than 0.
- Divide n by 4 until its possible to divide by 4.
- if n is 1 return true else false.
Code
class Solution {
public:
bool isPowerOfFour(int n) {
if(n>1)while(n%4==0) n/=4;
return n==1;
}
};
Bitwise Solution
- Power of 4, numbers have those 3 common features.
- greater than 0.
- Second,only have one β1β bit in their binary notation,so we use x&(x-1) to delete the lowest β1β,and if then it becomes 0,it prove that there is only one β1β bit.
- the only β1β bit should be locate at the odd location,for example,16.Itβs binary is 00010000.So we can use β0x55555555β to check if the β1β bit is in the right place.With this thought we can code it out easily!(0x55555555 is the hex representation of β1010101010101010101010101010101β)
Code
class Solution {
public:
bool isPowerOfFour(int num) {
return num > 0 && (num&(num-1)) == 0 && (num & 0x55555555) != 0;
}
};
MUST READ