Leetcode刷题笔记python---4的幂

4的幂

题目

给定一个整数 (32 位有符号整数),请编写一个函数来判断它是否是 4 的幂次方。

示例 1:

输入: 16
输出: true
示例 2:

输入: 5
输出: false


解答

思路:

  1. while
  2. 比较

代码:

class Solution(object):
    def isPowerOfFour(self, num):
        """
        :type num: int
        :rtype: bool
        """
        x=0
        while pow(4,x)<=num:
            if pow(4,x)-num==0:
                return True
            x+=1
        return False

结果:80%

猜你喜欢

转载自blog.csdn.net/sinat_29350597/article/details/83098463