1. 程式人生 > >Leetcode刷題筆記python---4的冪

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%