Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a8dac97

Browse files
committed
update: gen binary numbers using queue
1 parent 61049dc commit a8dac97

File tree

1 file changed

+29
-0
lines changed
  • src/_DataStructures_/Queue/generate-binary-number

1 file changed

+29
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
const Queue = require('../index');
2+
3+
function generateBinaryNumber(n) {
4+
const result = [];
5+
const q = new Queue();
6+
7+
// add `1` to the queue
8+
q.enqueue('1');
9+
10+
// iterate till the given number
11+
for (let i = 0; i < n; i += 1) {
12+
// push the item in the queue to the array
13+
result.push(q.dequeue());
14+
15+
// append `0` & `1` respectively
16+
const s1 = `${result[i]}0`;
17+
const s2 = `${result[i]}1`;
18+
19+
// push the combinations in the queue
20+
q.enqueue(s1);
21+
q.enqueue(s2);
22+
}
23+
// return the result containing all the binary numbers
24+
return result;
25+
}
26+
27+
// console.log(generateBinaryNumber(5));
28+
29+
module.exports = generateBinaryNumber;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /