Skip to content

Commit 0b5744e

Browse files
committed
剑指offer09 补充js版解法
1 parent 4e53da9 commit 0b5744e

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

animation-simulation/栈和队列/剑指Offer09用两个栈实现队列.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ class CQueue {
5858

5959
大家可以点击该链接[剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)去实现一下,下面我们看代码。
6060

61+
Java Code:
6162
```java
6263
class CQueue {
6364
//初始化两个栈
@@ -89,3 +90,24 @@ class CQueue {
8990
}
9091
```
9192

93+
JS Code:
94+
```javascript
95+
var CQueue = function() {
96+
this.stack1 = [];
97+
this.stack2 = [];
98+
};
99+
100+
CQueue.prototype.appendTail = function(value) {
101+
this.stack1.push(value);
102+
};
103+
104+
CQueue.prototype.deleteHead = function() {
105+
if (!this.stack2.length) {
106+
while(this.stack1.length) {
107+
this.stack2.push(this.stack1.pop());
108+
}
109+
}
110+
return this.stack2.pop() || -1;
111+
};
112+
```
113+

0 commit comments

Comments
 (0)