Skip to main content

1680. Concatenation of Consecutive Binary Numbers

https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/

Python

  • 直覺的寫法,但就run的過了...

  • Time: O(N) # 迴圈要從1跑到N

  • Space: O(1)

class Solution:
def concatenatedBinary(self, n: int) -> int:
mod = 10**9+7
ans = 1
for i in range(2, n+1):
bins = str(bin(i))[2:]
ans <<= len(bins)
ans += i
ans %= mod

return ans