mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2024-11-24 21:08:53 +00:00
剑指offer09 补充js版解法
This commit is contained in:
parent
4e53da9e3e
commit
0b5744e2fa
@ -58,6 +58,7 @@ class CQueue {
|
|||||||
|
|
||||||
大家可以点击该链接[剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)去实现一下,下面我们看代码。
|
大家可以点击该链接[剑指 Offer 09. 用两个栈实现队列](https://leetcode-cn.com/problems/yong-liang-ge-zhan-shi-xian-dui-lie-lcof/)去实现一下,下面我们看代码。
|
||||||
|
|
||||||
|
Java Code:
|
||||||
```java
|
```java
|
||||||
class CQueue {
|
class CQueue {
|
||||||
//初始化两个栈
|
//初始化两个栈
|
||||||
@ -89,3 +90,24 @@ class CQueue {
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
JS Code:
|
||||||
|
```javascript
|
||||||
|
var CQueue = function() {
|
||||||
|
this.stack1 = [];
|
||||||
|
this.stack2 = [];
|
||||||
|
};
|
||||||
|
|
||||||
|
CQueue.prototype.appendTail = function(value) {
|
||||||
|
this.stack1.push(value);
|
||||||
|
};
|
||||||
|
|
||||||
|
CQueue.prototype.deleteHead = function() {
|
||||||
|
if (!this.stack2.length) {
|
||||||
|
while(this.stack1.length) {
|
||||||
|
this.stack2.push(this.stack1.pop());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.stack2.pop() || -1;
|
||||||
|
};
|
||||||
|
```
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user