leetcode 225 补充js版解法

pull/20/head
daluozha 2021-04-27 18:13:08 +08:00
parent 39683f1794
commit 4e53da9e3e
1 changed files with 32 additions and 0 deletions

View File

@ -18,6 +18,7 @@
Java Code:
```java
class MyStack {
//初始化队列
@ -55,3 +56,34 @@ class MyStack {
```
JS Code:
```javascript
var MyStack = function() {
this.queue = [];
};
MyStack.prototype.push = function(x) {
this.queue.push(x);
if (this.queue.length > 1) {
let i = this.queue.length - 1;
while (i) {
this.queue.push(this.queue.shift());
i--;
}
}
};
MyStack.prototype.pop = function() {
return this.queue.shift();
};
MyStack.prototype.top = function() {
return this.empty() ? null : this.queue[0];
};
MyStack.prototype.empty = function() {
return !this.queue.length;
};
```