题目:
Given an integer, write a function to determine if it is a power of three.
Follow up:
Could you do it without using any loop / recursion?
分析:
一开始用了log(n)/log(3),但是在n=243的时候出现了错误,于是用了一个小trick。
代码:
class Solution {
public:
bool isPowerOfThree(int n) {
return n>0&&!(1162261467%n);
}
};