LeetCode. 3的幂

题目要求:

给定一个整数,写一个函数来判断它是否是 3 的幂次方。

示例:

输入: 27
输出: true

代码:

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;
    }
};

猜你喜欢

转载自www.cnblogs.com/leyang2019/p/11688936.html