LeetCode 231 Power of Two (Python)

Posted by 小明MaxMing on June 8, 2020

题目

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

解题思路

如果一个数n不为0,那么这个数是2的幂与n & (n - 1) = 0为充要条件

代码

class Solution:
    def isPowerOfTwo(self, n: int) -> bool:
        return n and not (n & (n - 1))

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅