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?
Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.
Subscribe to see which companies asked this question
说不用递归或循环,没有想到解法。
代码
class Solution {
public:
bool isPowerOfThree(int n) {
while(n>=3)
{
if(n%3!=0)
return false;
n=n/3;
}
return n==1;
}
};