-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Python solution for "1680. Concatenation of Consecutive Binary …
…Numbers"
- Loading branch information
1 parent
4a817b7
commit 7e6129d
Showing
2 changed files
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Solved: | ||
# (M) Concatenation of Consecutive Binary Numbers | ||
# https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/ | ||
|
||
class Solution: | ||
def concatenatedBinary(self, n: int) -> int: | ||
res = 1 | ||
shift = 1 | ||
modulo = 10**9 + 7 | ||
|
||
for i in range(2, n + 1): | ||
# If `i` is exact power of 2 increase `shift` by 1 | ||
if i & (i - 1) == 0: | ||
shift += 1 | ||
res = ((res << shift) + i) % modulo | ||
|
||
return res | ||
|
||
|
||
x = Solution() | ||
print(x.concatenatedBinary(1)) # 1 | ||
print(x.concatenatedBinary(3)) # 27 | ||
print(x.concatenatedBinary(12)) # 505379714 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters