美文网首页
231. Power of Two

231. Power of Two

作者: April63 | 来源:发表于2018-06-22 14:17 被阅读0次

黑人问号脸的嗯

class Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n < 0:
            return False
        if n == 0:
            return False
        if n == 1:
            return True
        while n > 1:
            if n % 2 != 0:
                return False
            n = n / 2
        return True

递归

class Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        if n < 1:
            return False
        if n == 1:
            return True
        if n % 2 != 0:
            return False
        return self.isPowerOfTwo(n / 2)

相关文章

网友评论

      本文标题:231. Power of Two

      本文链接:https://www.haomeiwen.com/subject/sjykyftx.html