栈与队列的相互模拟(leetcode225&&leetcode232)

一、用队列实现栈

1.1 Question

使用队列实现栈的下列操作:

push(x) – 元素 x 入栈
pop() – 移除栈顶元素
top() – 获取栈顶元素
empty() – 返回栈是否为空

注意:
你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty
这些操作是合法的。 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 ,
只要是标准的队列操作即可。 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

1.2 Analyze

压入(push)

当我们将一个元素从队列入队的时候,根据队列的性质这个元素会存在队列的后端。

但当我们实现一个栈的时候,最后入队的元素应该在前端,而不是在后端。为了实现这个目的,每当入队一个新元素的时候,我们可以把队列的顺序反转过来。

在这里插入图片描述

弹出(pop)

最后一个压入的元素永远在 q1 的前端,这样的话我们就能在常数时间内让它 出队。

1.3 代码实现(js)

// 使用队列实现栈的下列操作:
//
// push(x) -- 元素 x 入栈
// pop() -- 移除栈顶元素
// top() -- 获取栈顶元素
// empty() -- 返回栈是否为空


/**
 * 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.unshift(x);
    let size = this.queue.length;
    while(size>1){
        this.queue.unshift(this.queue.pop());
        size--;
    }
};

/**
 * Removes the element on top of the stack and returns that element.
 * @return {number}
 */
MyStack.prototype.pop = function() {
    return this.queue.pop();
};

/**
 * Get the top element.
 * @return {number}
 */
MyStack.prototype.top = function() {
    return this.queue[this.queue.length - 1];
};

/**
 * Returns whether the stack is empty.
 * @return {boolean}
 */
MyStack.prototype.empty = function() {
    return this.queue.length === 0;
};

/**
 * Your MyStack object will be instantiated and called as such:
 * var obj = new MyStack()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.top()
 * var param_4 = obj.empty()
 */

let st = new MyStack();
st.push(1);
st.push(2);
console.log(st.top());
console.log(st.pop());

二、用栈实现队列

1.1 Question

使用栈实现队列的下列操作:

push(x) – 将一个元素放入队列的尾部。
pop() – 从队列首部移除元素。
peek() – 返回队列首部的元素。
empty() – 返回队列是否为空。

1.2 Example

MyQueue queue = new MyQueue();

queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false

1.3 Anlyse

1、使用两个栈,一个栈(stackPush)用于元素进栈,一个栈(stackPop)用于元素出栈;

2、pop() 或者 peek() 的时候:

(1)如果 stackPop 里面有元素,直接从 stackPop 里弹出或者 peek 元素;

(2)如果 stackPop 里面没有元素,一次性将 stackPush 里面的所有元素倒入 stackPop。

为此,可以写一个 shift 辅助方法,一次性将 stackPush 里的元素倒入 stackPop。

from:liweiwei1419

1.4 代码(js)

// 使用栈实现队列的下列操作:
//
// push(x) -- 将一个元素放入队列的尾部。
// pop() -- 从队列首部移除元素。
// peek() -- 返回队列首部的元素。
// empty() -- 返回队列是否为空。



/**
 * Initialize your data structure here.
 */
var MyQueue = function() {
    this.stackPop = [];
    this.stackPush = [];
};

/**
 * Push element x to the back of queue.
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function(x) {
    this.stackPush.push(x);
};

/**
 * Removes the element from in front of queue and returns that element.
 * @return {number}
 */
MyQueue.prototype.pop = function() {
    if(this.stackPop.length === 0){
        this.poll();
    }
    return this.stackPop.pop();
};

/**
 * Get the front element.
 * @return {number}
 */
MyQueue.prototype.peek = function() {
    if(this.stackPop.length === 0){
        this.poll();
    }
    return this.stackPop[this.stackPop.length - 1];
};

MyQueue.prototype.poll = function(){
    while(this.stackPush.length !== 0){
        this.stackPop.push(this.stackPush.pop());
    }
};

/**
 * Returns whether the queue is empty.
 * @return {boolean}
 */
MyQueue.prototype.empty = function() {
    return this.stackPop.length === 0 && this.stackPush.length === 0;
};

/**
 * Your MyQueue object will be instantiated and called as such:
 * var obj = new MyQueue()
 * obj.push(x)
 * var param_2 = obj.pop()
 * var param_3 = obj.peek()
 * var param_4 = obj.empty()
 */

All from Leetcode

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章