Given an integer, write a function to determine if it is a power of three.
方法一:
class Solution {
public:
bool isPowerOfThree(int n) {
if(n<=0)
return false;
if(n==1)
return true;
if(n>1 && n%3==0)
return isPowerOfThree(n/3);
else
return false;
}
};
方法二:
class Solution {
public:
bool isPowerOfThree(int n) {
if(n<=0)
return false;
while(n%3==0)
n=n/3;
return n==1;
}
};