toBinaryString

Easy · Bit Manipulation · 4 test cases

Given a non-negative integer, return its binary representation as a string.

Examples

toBinaryString(5) → '101'
toBinaryString(10) → '1010'

Starter code

function toBinaryString(n) {

}

Complexity of the optimal solution

Time O(log n), space O(log n). The built-in `toString(2)` method is efficient. The number of digits in the binary representation of n is proportional to log n. Therefore, both the time to generate the string and the space to store it are O(log n).

Solve toBinaryString in the browser