LeetCode. Power 3

Topics requirements:

Given an integer, write a function to determine whether it is a power of 3.

Example:

Input: 27
Output: true

Code:

class Solution {
public:
    bool isPowerOfThree(int n) {
        double tmp = n / 1.0;
        while(tmp >= 3.0) {
            tmp /= 3.0;
        }
        
        if(tmp == 1.0) {
            return true;
        }
        return false;
    }
};

Guess you like

Origin www.cnblogs.com/leyang2019/p/11688936.html