mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-10 20:40:39 +08:00
添加225. 用队列实现栈JavaScript版本
This commit is contained in:
@ -357,8 +357,115 @@ class MyStack:
|
|||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
|
javaScript:
|
||||||
|
|
||||||
|
使用数组(push, shift)模拟队列
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
// 使用两个队列实现
|
||||||
|
/**
|
||||||
|
* Initialize your data structure here.
|
||||||
|
*/
|
||||||
|
var MyStack = function() {
|
||||||
|
this.queue1 = [];
|
||||||
|
this.queue2 = [];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push element x onto stack.
|
||||||
|
* @param {number} x
|
||||||
|
* @return {void}
|
||||||
|
*/
|
||||||
|
MyStack.prototype.push = function(x) {
|
||||||
|
this.queue1.push(x);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the element on top of the stack and returns that element.
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
MyStack.prototype.pop = function() {
|
||||||
|
// 减少两个队列交换的次数, 只有当queue1为空时,交换两个队列
|
||||||
|
if(!this.queue1.length) {
|
||||||
|
[this.queue1, this.queue2] = [this.queue2, this.queue1];
|
||||||
|
}
|
||||||
|
while(this.queue1.length > 1) {
|
||||||
|
this.queue2.push(this.queue1.shift());
|
||||||
|
}
|
||||||
|
return this.queue1.shift();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the top element.
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
MyStack.prototype.top = function() {
|
||||||
|
const x = this.pop();
|
||||||
|
this.queue1.push(x);
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the stack is empty.
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
|
MyStack.prototype.empty = function() {
|
||||||
|
return !this.queue1.length && !this.queue2.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
```js
|
||||||
|
|
||||||
|
// 使用一个队列实现
|
||||||
|
/**
|
||||||
|
* Initialize your data structure here.
|
||||||
|
*/
|
||||||
|
var MyStack = function() {
|
||||||
|
this.queue = [];
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Push element x onto stack.
|
||||||
|
* @param {number} x
|
||||||
|
* @return {void}
|
||||||
|
*/
|
||||||
|
MyStack.prototype.push = function(x) {
|
||||||
|
this.queue.push(x);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Removes the element on top of the stack and returns that element.
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
MyStack.prototype.pop = function() {
|
||||||
|
let size = this.queue.length;
|
||||||
|
while(size-- > 1) {
|
||||||
|
this.queue.push(this.queue.shift());
|
||||||
|
}
|
||||||
|
return this.queue.shift();
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the top element.
|
||||||
|
* @return {number}
|
||||||
|
*/
|
||||||
|
MyStack.prototype.top = function() {
|
||||||
|
const x = this.pop();
|
||||||
|
this.queue.push(x);
|
||||||
|
return x;
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns whether the stack is empty.
|
||||||
|
* @return {boolean}
|
||||||
|
*/
|
||||||
|
MyStack.prototype.empty = function() {
|
||||||
|
return !this.queue.length;
|
||||||
|
};
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
-----------------------
|
-----------------------
|
||||||
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
* 作者微信:[程序员Carl](https://mp.weixin.qq.com/s/b66DFkOp8OOxdZC_xLZxfw)
|
||||||
|
Reference in New Issue
Block a user