Leetcode Problem 1680. Concatenation of Consecutive Binary Numbers

1680. Concatenation of Consecutive Binary Numbers

Leetcode Solutions

Math (Bitwise Operation)

  1. Initialize result to 0 and MOD to 1000000007 (10^9 + 7).
  2. Initialize length to 0, which will keep track of the current length of the binary representation.
  3. Iterate i from 1 to n: a. If i is a power of 2, increment length (since the binary representation of i will be one bit longer). b. Shift result to the left by length bits. c. Use bitwise OR to add i to result. d. Apply modulo MOD to keep the result within the integer range.
  4. Return result after the loop.
UML Thumbnail

Change to Binary String

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...