Skip to content

Commit 54dcb0f

Browse files
authored
feat: add javascript solution to lc problem: No.1670.Design Front Middle Back Queue (doocs#416)
1 parent 7c02c0a commit 54dcb0f

File tree

3 files changed

+265
-0
lines changed

3 files changed

+265
-0
lines changed

solution/1600-1699/1670.Design Front Middle Back Queue/README.md

+90
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,96 @@ class FrontMiddleBackQueue {
220220
*/
221221
```
222222

223+
### **JavaScript**
224+
225+
```js
226+
var FrontMiddleBackQueue = function() {
227+
this.left = [];
228+
this.right = [];
229+
};
230+
231+
/**
232+
* @param {number} val
233+
* @return {void}
234+
*/
235+
FrontMiddleBackQueue.prototype.pushFront = function(val) {
236+
this.left.unshift(val);
237+
this.rebalance();
238+
};
239+
240+
/**
241+
* @param {number} val
242+
* @return {void}
243+
*/
244+
FrontMiddleBackQueue.prototype.pushMiddle = function(val) {
245+
this.left.push(val);
246+
this.rebalance();
247+
};
248+
249+
/**
250+
* @param {number} val
251+
* @return {void}
252+
*/
253+
FrontMiddleBackQueue.prototype.pushBack = function(val) {
254+
this.right.push(val);
255+
this.rebalance();
256+
};
257+
258+
/**
259+
* @return {number}
260+
*/
261+
FrontMiddleBackQueue.prototype.popFront = function() {
262+
if (this.isEmpty()) return -1;
263+
let num = this.left.length == 0 ? this.right.shift() : this.left.shift();
264+
this.rebalance();
265+
return num;
266+
};
267+
268+
/**
269+
* @return {number}
270+
*/
271+
FrontMiddleBackQueue.prototype.popMiddle = function() {
272+
if (this.isEmpty()) return -1;
273+
let num = this.left.length == this.right.length ? this.left.pop() : this.right.shift();
274+
this.rebalance();
275+
return num;
276+
};
277+
278+
/**
279+
* @return {number}
280+
*/
281+
FrontMiddleBackQueue.prototype.popBack = function() {
282+
if (this.isEmpty()) return -1;
283+
let num = this.right.pop();
284+
this.rebalance();
285+
return num;
286+
};
287+
288+
FrontMiddleBackQueue.prototype.rebalance = function () {
289+
while (this.left.length > this.right.length) {
290+
this.right.unshift(this.left.pop());
291+
}
292+
while (this.right.length > this.left.length + 1) {
293+
this.left.push(this.right.shift());
294+
}
295+
}
296+
297+
FrontMiddleBackQueue.prototype.isEmpty = function () {
298+
return this.left.length == 0 && this.right.length == 0;
299+
}
300+
301+
/**
302+
* Your FrontMiddleBackQueue object will be instantiated and called as such:
303+
* var obj = new FrontMiddleBackQueue()
304+
* obj.pushFront(val)
305+
* obj.pushMiddle(val)
306+
* obj.pushBack(val)
307+
* var param_4 = obj.popFront()
308+
* var param_5 = obj.popMiddle()
309+
* var param_6 = obj.popBack()
310+
*/
311+
```
312+
223313
### **...**
224314

225315
```

solution/1600-1699/1670.Design Front Middle Back Queue/README_EN.md

+90
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,96 @@ class FrontMiddleBackQueue {
208208
*/
209209
```
210210

211+
### **JavaScript**
212+
213+
```js
214+
var FrontMiddleBackQueue = function() {
215+
this.left = [];
216+
this.right = [];
217+
};
218+
219+
/**
220+
* @param {number} val
221+
* @return {void}
222+
*/
223+
FrontMiddleBackQueue.prototype.pushFront = function(val) {
224+
this.left.unshift(val);
225+
this.rebalance();
226+
};
227+
228+
/**
229+
* @param {number} val
230+
* @return {void}
231+
*/
232+
FrontMiddleBackQueue.prototype.pushMiddle = function(val) {
233+
this.left.push(val);
234+
this.rebalance();
235+
};
236+
237+
/**
238+
* @param {number} val
239+
* @return {void}
240+
*/
241+
FrontMiddleBackQueue.prototype.pushBack = function(val) {
242+
this.right.push(val);
243+
this.rebalance();
244+
};
245+
246+
/**
247+
* @return {number}
248+
*/
249+
FrontMiddleBackQueue.prototype.popFront = function() {
250+
if (this.isEmpty()) return -1;
251+
let num = this.left.length == 0 ? this.right.shift() : this.left.shift();
252+
this.rebalance();
253+
return num;
254+
};
255+
256+
/**
257+
* @return {number}
258+
*/
259+
FrontMiddleBackQueue.prototype.popMiddle = function() {
260+
if (this.isEmpty()) return -1;
261+
let num = this.left.length == this.right.length ? this.left.pop() : this.right.shift();
262+
this.rebalance();
263+
return num;
264+
};
265+
266+
/**
267+
* @return {number}
268+
*/
269+
FrontMiddleBackQueue.prototype.popBack = function() {
270+
if (this.isEmpty()) return -1;
271+
let num = this.right.pop();
272+
this.rebalance();
273+
return num;
274+
};
275+
276+
FrontMiddleBackQueue.prototype.rebalance = function () {
277+
while (this.left.length > this.right.length) {
278+
this.right.unshift(this.left.pop());
279+
}
280+
while (this.right.length > this.left.length + 1) {
281+
this.left.push(this.right.shift());
282+
}
283+
}
284+
285+
FrontMiddleBackQueue.prototype.isEmpty = function () {
286+
return this.left.length == 0 && this.right.length == 0;
287+
}
288+
289+
/**
290+
* Your FrontMiddleBackQueue object will be instantiated and called as such:
291+
* var obj = new FrontMiddleBackQueue()
292+
* obj.pushFront(val)
293+
* obj.pushMiddle(val)
294+
* obj.pushBack(val)
295+
* var param_4 = obj.popFront()
296+
* var param_5 = obj.popMiddle()
297+
* var param_6 = obj.popBack()
298+
*/
299+
```
300+
211301
### **...**
212302

213303
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
var FrontMiddleBackQueue = function() {
2+
this.left = [];
3+
this.right = [];
4+
};
5+
6+
/**
7+
* @param {number} val
8+
* @return {void}
9+
*/
10+
FrontMiddleBackQueue.prototype.pushFront = function(val) {
11+
this.left.unshift(val);
12+
this.rebalance();
13+
};
14+
15+
/**
16+
* @param {number} val
17+
* @return {void}
18+
*/
19+
FrontMiddleBackQueue.prototype.pushMiddle = function(val) {
20+
this.left.push(val);
21+
this.rebalance();
22+
};
23+
24+
/**
25+
* @param {number} val
26+
* @return {void}
27+
*/
28+
FrontMiddleBackQueue.prototype.pushBack = function(val) {
29+
this.right.push(val);
30+
this.rebalance();
31+
};
32+
33+
/**
34+
* @return {number}
35+
*/
36+
FrontMiddleBackQueue.prototype.popFront = function() {
37+
if (this.isEmpty()) return -1;
38+
let num = this.left.length == 0 ? this.right.shift() : this.left.shift();
39+
this.rebalance();
40+
return num;
41+
};
42+
43+
/**
44+
* @return {number}
45+
*/
46+
FrontMiddleBackQueue.prototype.popMiddle = function() {
47+
if (this.isEmpty()) return -1;
48+
let num = this.left.length == this.right.length ? this.left.pop() : this.right.shift();
49+
this.rebalance();
50+
return num;
51+
};
52+
53+
/**
54+
* @return {number}
55+
*/
56+
FrontMiddleBackQueue.prototype.popBack = function() {
57+
if (this.isEmpty()) return -1;
58+
let num = this.right.pop();
59+
this.rebalance();
60+
return num;
61+
};
62+
63+
FrontMiddleBackQueue.prototype.rebalance = function () {
64+
while (this.left.length > this.right.length) {
65+
this.right.unshift(this.left.pop());
66+
}
67+
while (this.right.length > this.left.length + 1) {
68+
this.left.push(this.right.shift());
69+
}
70+
}
71+
72+
FrontMiddleBackQueue.prototype.isEmpty = function () {
73+
return this.left.length == 0 && this.right.length == 0;
74+
}
75+
76+
/**
77+
* Your FrontMiddleBackQueue object will be instantiated and called as such:
78+
* var obj = new FrontMiddleBackQueue()
79+
* obj.pushFront(val)
80+
* obj.pushMiddle(val)
81+
* obj.pushBack(val)
82+
* var param_4 = obj.popFront()
83+
* var param_5 = obj.popMiddle()
84+
* var param_6 = obj.popBack()
85+
*/

0 commit comments

Comments
 (0)