LeaCoder

LeetCode-231--2的幂(Power-of-Two)

2019-04-16
leacoder

LeetCode.jpg

231. 2的幂

给定一个整数,编写一个函数来判断它是否是 2 的幂次方。

示例 1:

    输入: 1

    输出: true

    解释: 20 = 1

示例 2:

    输入: 16

    输出: true

    解释: 24 = 16

示例 3:

    输入: 218

    输出: false

Python3 实现

# @author:leacoder 
# @des: 位运算  2的幂

class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        if n != 0 and n & (n-1) == 0:
            return True
        else:
            return False

GitHub链接: https://github.com/lichangke/LeetCode

知乎个人首页: https://www.zhihu.com/people/lichangke/

简书个人首页: https://www.jianshu.com/u/3e95c7555dc7

个人Blog: https://lichangke.github.io/

欢迎大家来一起交流学习


Similar Posts

Comments