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@dietpepsifor adding this problem and creating all test cases.
Subscribeto see which companies asked this question
//給定一個整數,編寫函數判斷它是否為3的冪
class Solution {
public:
bool isPowerOfThree(int n) {
if(n>1)
{
while(n%3==0)
n=n/3;
}
return n==1;
}
};