美文网首页
326. Power of Three

326. Power of Three

作者: YellowLayne | 来源:发表于2017-11-01 14:08 被阅读0次

1.描述

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?

2.分析

3.代码

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

相关文章

网友评论

      本文标题:326. Power of Three

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