326. 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?

这题要能面试到,我吃屎。。

class Solution {
    public boolean isPowerOfThree(int n) {
        if(n ==1) return true;
        while(n%3 == 0 && n>3){
            n = n/3;
        }
        if(n ==3){
            return true;
        }
        else{
            return false;
        }
    }
}

This site Original article All followed" Attribution—NonCommercial—ShareAlike 4.0 (CC BY-NC-SA 4.0) ”。 Please keep the following marks for sharing and interpretation:

Original author: Jake Tao Source: 「326. Power of Three」

Praise 164
0 1 164

Further reading

Post a reply

Log in can only be commented on later

评论列表(1条)

  • tt
    tt 2018-04-18 00:51

    like your comment.

Share this page
Back to top