leetcode (Power of Three)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hsx1612727380/article/details/85332375

Title:Power of Three     326

Difficulty:Easy

原题leetcode地址:   https://leetcode.com/problems/power-of-three/

1.  转换log函数求解

时间复杂度:O(1),没有循环、递归。

空间复杂度:O(1),没有申请额外空间。

    /**
     * 转换log函数求解
     * @param n
     * @return
     */
    public static boolean isPowerOfThree(int n) {

        if (n <= 0) {
            return false;
        }

        if (n == Math.pow(3, Math.round(Math.log(n) / Math.log(3)))) {
            return true;
        }

        return false;

    }

猜你喜欢

转载自blog.csdn.net/hsx1612727380/article/details/85332375