Skip to content

Commit 4e53da9

Browse files
committed
leetcode 225 补充js版解法
1 parent 39683f1 commit 4e53da9

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

animation-simulation/栈和队列/225.用队列实现栈.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
下面我们来看一下题目代码,也是很容易理解。
2020

21+
Java Code:
2122
```java
2223
class MyStack {
2324
//初始化队列
@@ -55,3 +56,34 @@ class MyStack {
5556

5657
```
5758

59+
JS Code:
60+
```javascript
61+
var MyStack = function() {
62+
this.queue = [];
63+
};
64+
65+
MyStack.prototype.push = function(x) {
66+
this.queue.push(x);
67+
if (this.queue.length > 1) {
68+
let i = this.queue.length - 1;
69+
while (i) {
70+
this.queue.push(this.queue.shift());
71+
i--;
72+
}
73+
}
74+
};
75+
76+
MyStack.prototype.pop = function() {
77+
return this.queue.shift();
78+
};
79+
80+
MyStack.prototype.top = function() {
81+
return this.empty() ? null : this.queue[0];
82+
83+
};
84+
85+
MyStack.prototype.empty = function() {
86+
return !this.queue.length;
87+
};
88+
```
89+

0 commit comments

Comments
 (0)