231. Power of two

Input: n = 1
Output: true
Explanation: 20 = 1
Input: n = 16
Output: true
Explanation: 24 = 16
Input: n = 3
Output: false
  • -231 <= n <= 231 - 1
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
if(n<0):
return False
cnt=0
while(n):
if(n&1==1):
cnt+=1
n>>=1
if(cnt==1):
return True
return False
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
i=1
while(i<n):
i*=2
return(i==n)
class Solution:
def isPowerOfTwo(self, n: int) -> bool:
return(False if n<=0 else math.log2(n).is_integer())

--

--

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
Dhanarajappu

Dhanarajappu

12 Followers

Tech-Enthusiast, Coder,Explorer,Geeky,Software Engineer |A piece of code delivers everything that you need. The world is all about codes.