Skip to content

Commit 0957fea

Browse files
authored
Create concatenation-of-consecutive-binary-numbers.py
1 parent 935815a commit 0957fea

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def concatenatedBinary(self, n):
6+
"""
7+
:type n: int
8+
:rtype: int
9+
"""
10+
MOD = 10**9+7
11+
result = l = 0
12+
for i in xrange(1, n+1):
13+
if i&(i-1) == 0:
14+
l += 1
15+
result = ((result<<l)%MOD+i)%MOD
16+
return result

0 commit comments

Comments
 (0)