leetcode 326. Power of Three 3的幂 python math(一行代码)

class Solution:
    def isPowerOfThree(self, n):
        """
        :type n: int
        :rtype: bool
        """



        # Approach #2 低效的循环
        # if n :
        #     while n % 3 == 0:
        #         n /= 3
        #     return n == 1
        # return False


        # Approach #3  2**31 = 2147483648
        return n > 0 and 1162261467 % n == 0

猜你喜欢

转载自blog.csdn.net/huhehaotechangsha/article/details/80899139