题目描述
Given an integer, write a function to determine if it is a power of three.
题目大意
判断给的整数是否是3的乘方数。
(尽量不要用循环和递归来完成算法)
示例
E1
E2
E3
E4
解题思路
log10n / log103应该为整数。
复杂度分析
时间复杂度:O(1)
空间复杂度:O(1)
代码
class Solution { public: bool isPowerOfThree(int n) { return fmod(log10(n)/log10(3), 1)==0; } };