leetcode 225 补充js版解法

This commit is contained in:
daluozha 2021-04-27 18:13:08 +08:00
parent 39683f1794
commit 4e53da9e3e

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;
};
```