326. power of three

Given an integer, write a function to determine if it is a power of three.

题意分析:

n^m,n叫做幂,m叫做次方。32位int型,当n == 19是,3^n最大。
因此要判断一个数num是否为a power of three,可以通过3 ^ n % num。

1
2
3
4
5
6
class Solution {
public:
bool (int n) {
return n <= 0 ? false : (((int)pow(3,19)) % n == 0);
}
};