[LeetCode]#231. Power of Two

Input: 1
Output: true
Explanation: 20 = 1
Input: 16
Output: true
Explanation: 24 = 16
Input: 218
Output: false
class Solution:
def isPowerOfTwo(self, n):
if n == 1:
return True
elif n==0:
return False
elif n % 2 != 0:
return False
else:
return self.isPowerOfTwo(n/2)

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store