Leetcode 899 Solution
This article provides solution to leetcode question 899 (binary-gap)
Access this page by simply typing in "lcs 899" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
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)