[LeetCode326] Power of Three

题目:

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?

分类:Math 

代码:

 1 class Solution {
 2 public:
 3     bool isPowerOfThree(int n) {
 4         if(n <= 0)
 5             return false;
 6         double res = log10(n) / log10(3);
 7         //floor(x)返回小于x的最大整数 如floor(1.2) = 1
 8         //ceil(x)返回的是大于x的最小整数 ceil(1.2) = 2
 9         return res == floor(res);
10     }
11 };

 

posted @ 2016-08-14 22:29  zhangbaochong  阅读(152)  评论(0)    收藏  举报