C++ accepted solution


  • 0
    J

    The basic idea of this solution is similar to other solutions. I made use of ceil and floor functions to evaluate the result.

    bool isPowerOfThree(int n) {
        double d = log10(n) / log10(3);
        return n > 0 && ceil(d) == floor(d);
    }

  • 0
    J

    it fails if we use log() instead of log10(), why?


  • 0
    J

    I tested log() instead of log10(), and found that the the precision of the variable d generates the error. We can anyway come up with this issue by changing the precision, but because of this issue, I have to admit that this solution is not accurate enough to solve this problem.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.