码迷,mamicode.com
首页 > 其他好文 > 详细

leetcode 231. Power of Two

时间:2018-04-16 23:52:20      阅读:176      评论:0      收藏:0      [点我收藏+]

标签:leetcode   func   false   UNC   nbsp   min   isp   return   pow   

Given an integer, write a function to determine if it is a power of two.

class Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        #-4???
        if n >= 1:
            return n & (n-1) == 0
        else:
            return False

 

class Solution(object):
    def isPowerOfTwo(self, n):
        """
        :type n: int
        :rtype: bool
        """
        #-4???
        c = 0
        while n > 0:
            if n & 1:
                c += 1
            n >>= 1
        return c == 1                    

 

leetcode 231. Power of Two

标签:leetcode   func   false   UNC   nbsp   min   isp   return   pow   

原文地址:https://www.cnblogs.com/bonelee/p/8859050.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!