Given an integer, write a function to determine if it is a power of three.
Solution 1:
class Solution {
public:
bool isPowerOfThree(int n) {
int arr[] = {1,3, 9, 27, 81, 243, 729, 2187, 6561, 19683, 59049, 177147, 531441, 1594323, 4782969, 14348907, 43046721, 129140163, 387420489, 1162261467, 3486784401, 10460353203};
set<int> s = set<int>(arr, arr+21);
return s.count(n) == 1;
}
};
Solution 2:
class Solution {
public:
bool isPowerOfThree(int n) {
return n > 0 && 1162261467 % n == 0;
}
};