Skip to content
LC-0231 Easy LeetCode

231. Power of Two

Read the full problem statement on LeetCode.
Difficulty: easy Acceptance: 48% Topics: Math, Bit Manipulation, Recursion
View full problem on LeetCode
Reference solution (spoiler · python)
# Time:  O(1)
# Space: O(1)

class Solution(object):
    # @param {integer} n
    # @return {boolean}
    def isPowerOfTwo(self, n):
        return n > 0 and (n & (n - 1)) == 0


class Solution2(object):
    # @param {integer} n
    # @return {boolean}
    def isPowerOfTwo(self, n):
        return n > 0 and (n & ~-n) == 0

Solution from kamyu104/LeetCode-Solutions · MIT