美文网首页
[LeetCode 326] Power of Three

[LeetCode 326] Power of Three

作者: FTVBeginning | 来源:发表于2016-04-28 16:26 被阅读0次

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?

Method:
Power of Three does not follow the similar rule of power of four. We can use log operation to solve this problem. However, attention should be paid to accuracy of numbers.

C++:
class Solution {
public:
bool isPowerOfThree(int n) {
double ans = log10(n)/log10(3);
return ans-int(ans)==0;
}
};

相关文章

网友评论

      本文标题:[LeetCode 326] Power of Three

      本文链接:https://www.haomeiwen.com/subject/iddhrttx.html