题目链接:868. 二进制间距
直接计算。
class Solution:
def binaryGap(self, n: int) -> int:
indices = [i for i in range(32) if (1 << i) & n != 0]
if len(indices) < 2:
return 0
return max(r - l for l, r in zip(indices[:-1], indices[1:]))