【leetcode】231.Power of Two

题目描述
求n是否是2的n次方。

思路
使用位运算
假如n是2的n次方,则n&n-1一定等于0.

代码

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

猜你喜欢

转载自blog.csdn.net/qq_42011358/article/details/83574523
今日推荐