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?
class Solution {
public:
bool isPowerOfFour(int num) {
unordered_set<int> set;
int p=1;
set.insert(p);
for (int i=2;i<=31;i=i+2){
p<<=2;
set.insert(p);
//cout<<p<<endl;
}
if (set.find(num)!=set.end())
return true;
else
return false;
}
};