check (n>>1)&1 to count the number of 1s


  • 0
    class Solution(object):
        def isPowerOfTwo(self, n):
            if not n>0: return False
            while n:
                if n&1: break
                n = n>>1
            return n==1
    

Log in to reply
 

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