LeetCode : 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?

Credits:
Special thanks to @dietpepsi for adding this problem and creating all test cases.

Subscribe to see which companies asked this question.

class Solution {
public:
    bool isPowerOfThree(int n) {
        if(n<=0)
           return false;
        if(n==1)
            return 1;
        else if(n%3==0)
            return isPowerOfThree(n/3);
        else
            return false;
    }
};

posted on 2017-03-13 23:26  gechen  阅读(72)  评论(0编辑  收藏  举报

导航