Leetcode 899 Solution

This article provides solution to leetcode question 899 (binary-gap)

https://leetcode.com/problems/binary-gap

Solution

class Solution(object):
    def binaryGap(self, N):
        """
        :type N: int
        :rtype: int
        """
        last_val = None
        ans = 0

        while N:
            next_N = N & (N - 1)
            val = N - next_N
            if last_val:
                ans = max(ans, math.log(val / last_val, 2.0))
            last_val = val
            N = next_N

        return int(ans)