1. 程式人生 > >leetcode 231. 2的冪 【Easy】

leetcode 231. 2的冪 【Easy】

題目:

給定一個整數,編寫一個函式來判斷它是否是 2 的冪次方。

示例 1:

輸入: 1
輸出: true
解釋: 20 = 1

示例 2:

輸入: 16
輸出: true
解釋: 24 = 16

示例 3:

輸入: 218
輸出: false

程式碼:

class Solution:
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        init = 1
        if init == n:
            return True
        while init<n:
            init *= 2
            if init == n:
                return True
        return False