Concatenation of Consecutive Binary Numbers LeetCode solution in Python

1
class Solution:
2
def concatenatedBinary(self, n: int) -> int:
3
modulo = 10**9 + 7
4
shift = 0 # tracking power of 2
5
res = 0
6

7
for i in range(1, n + 1):
8
if i & (i - 1) == 0: # see if num reaches a greater power of 2
9
shift += 1
10
res = ((res << shift) + i) % modulo # do the simulation
11

12
return res

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0