# My accepted oneline python solution.

• ``````class Solution:
# @param {integer} n
# @return {boolean}
def isPowerOfTwo(self, n):
return True if n == 1 else (False if n == 0 else n % 2 == 0 and self.isPowerOfTwo(n/2))``````

• Same thing:

``````def isPowerOfTwo(self, n):
return n == 1 or n != 0 and n % 2 == 0 and self.isPowerOfTwo(n/2)
``````

Bit shortened:

``````def isPowerOfTwo(self, n):
return n == 1 or n > 0 == n % 2 and self.isPowerOfTwo(n/2)
``````

• I'm sorry, but Y is self.isPowerOfTwo(n/2)? I don't know what the relationship between n and n/2 in this function.

• What is "Y"?

• this is better! Thanks!

• I'm sorry, but why is self.isPowerOfTwo(n/2)? I don't know what the relationship between n and n/2 in this function.

Sorry, I mean I don't know why self.isPowerOfTwo(n/2) makes sense. eg, if n = 27, n cannot be divided by 2.

• 27 certainly can be divided by 2. The result is 13. It wouldn't be done, though, since the `n % 2 == 0` would fail before and thus it wouldn't even come to that division.

This approach analyzes the bit pattern, and dividing by 2 is the same as shifting to the right by one bit.

• Thank you! I think I understand it.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.