C++ easy understand solutions (w/ & w/o loop)


  • 2
    D
    class Solution {
    public:
        bool isPowerOfThree(int n) {
            // With Loop Version
            //if(n <= 0) return false;
            //while(n%3 == 0){
            //     n = n/3;
            // }
            // if(n != 1) return false;
            // else return true;
            
            // Without Loop Version
            if(n <= 0) return false;
            return (pow(3,(int)(log10(n)/log10(3))) == n);
        }
    };`

  • 0
    L

    I'm a little confused! When I replace 'log10' with 'log',the result is wrong.


Log in to reply
 

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