Solution 1

  • TimeO(1)
  • SpaceO(1)

Python generates only 32 bits for integers, so the length is usually fixed
Solved in 5 mins, all by yourself! Great job!

Python · 2026-02-22
class Solution:
    '''
    Time Complexity: O(1)
    Space Complexity: O(1)
    Python generates only 32 bits for integers, so the length is usually fixed
    Solved in 5 mins, all by yourself! Great job!
    '''
    def binaryGap(self, n: int) -> int:
        b = bin(n)[2:]
        m = 0
        l = 0
        for r in range(len(b)):
            if b[r] == '1':
                m = max(m, r-l)
                l = r
        return m
Leet Code/python.py · L5402–5418