581. Generate Binary Numbers from 1 to N

EasyStackQueue

Given an integer n, generate the binary representations of the numbers from 1 to n in order and return them as a list of strings. A queue-based approach appends '0' and '1' to previously generated numbers. The input is JSON {n}.

Input: JSON {n}.

Output: Array — binary strings for 1..n.

Examples

Example 1
Input: {"n":5}
Output: ["1","10","11","100","101"]
Explanation: Binary of 1 through 5.
Example 2
Input: {"n":1}
Output: ["1"]
Explanation: Just 1.
Example 3
Input: {"n":3}
Output: ["1","10","11"]
Explanation: Binary of 1,2,3.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →