po主第一篇LeetCode練習
題:
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?
意思就是判斷一個數是不是3的冪次方,儘量不用循環與遞歸?
solution:
使用遞歸或者循環這道題應該是沒有難度的,但是不用循環和遞歸需要轉個腦筋。
判斷x是不是一個整數即可
class Solution {
public:
bool isPowerOfThree(int n) {
double m = log10(n)/log10(3);
if(m-int(m)==0)return true;
return false;
}
};