Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
A01 Implement Queue with Limited Size of Arrays
/* A01 Implement Queue with Limited Size of Arrays 解法: 用2-D array */ var Queue = function(arraySize){ this.arraySize = arraySize; this.q = [[]] this.used = 0 ; } Queue.prototype.push = function(num){ let len = this.q.length; let arr = this.q[len-1] arr.push(num); this.used++; if(arr.length == this.arraySize){ this.q.push([]); } } Queue.prototype.shift =function(){ if(this.used > 0){ this.used--; let res = this.q[0].shift(); if(this.q[0].length == 0 && this.used >0) { this.q.shift(); // get rid of the empty [] } return res; } else{ return null; } } Queue.prototype.getFront = function(){ if(this.used > 0) return this.q[0][0]; else return null; } // let q = new Queue(3) console.log(q) q.push(1); q.push(2); q.push(3) console.log(q) q.push(4); q.push(5); console.log(q) let x = q.shift() console.log("x=", x) console.log(q) console.log("front = ", q.getFront()); q.shift() q.shift() console.log(q) q.shift() console.log(q) q.shift() q.shift() q.shift() console.log(q )
run
|
edit
|
history
|
help
0
Add property to object
myfirstmodule.js
Compare 2 arrays and fill 1 or 2
neural spam network
fast function to reverse unsigned integer
localhost:8080/?name=2017&lname=July
To compare; creating async methods in javascript
Funny stuff
apply
Count digits