LeetCode刷题 Day10

112 阅读2分钟

LeetCode刷题 Day10

232. Implement Queue using Stacks

Implement a first in first out (FIFO) queue using only two stacks. The implemented queue should support all the functions of a normal queue (pushpeekpop, and empty).

Implement the MyQueue class:

  • void push(int x) Pushes element x to the back of the queue.
  • int pop() Removes the element from the front of the queue and returns it.
  • int peek() Returns the element at the front of the queue.
  • boolean empty() Returns true if the queue is empty, false otherwise.

Notes:

  • You must use only standard operations of a stack, which means only push to toppeek/pop from topsize, and is empty operations are valid.
  • Depending on your language, the stack may not be supported natively. You may simulate a stack using a list or deque (double-ended queue) as long as you use only a stack's standard operations.

Example 1:

Input
["MyQueue", "push", "push", "peek", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 1, 1, false]

Explanation
MyQueue myQueue = new MyQueue();
myQueue.push(1); // queue is: [1]
myQueue.push(2); // queue is: [1, 2] (leftmost is front of the queue)
myQueue.peek(); // return 1
myQueue.pop(); // return 1, queue is [2]
myQueue.empty(); // return false

思路:

  • 要想清楚stack,queue, array (js)的操作, 因为要把array当成stack来模拟queue
  • 这意味着array只能做push和pop,不能用shift和unshift, 因为stack没有这种操作
  • push, 直接push到stackIn
  • pop, 判断stackOut是否为空,如果为空,则pop空stackIn,全部push进stackOut, 最后return stackOut.pop()
  • peek, 执行已经实现的pop操作, 再push回stackOut. return pop value
  • empty, return stackIn & stackOut length === 0

代码:

    var MyQueue = function() {
    this.stackIn = [];
    this.stackOut = [];
};

/** 
 * @param {number} x
 * @return {void}
 */
MyQueue.prototype.push = function(x) {
    this.stackIn.push(x);
};

/**
 * @return {number}
 */
MyQueue.prototype.pop = function() {
    if (!this.stackOut.length) {
        while (this.stackIn.length > 0) {
            this.stackOut.push(this.stackIn.pop());
        }
    }

    return this.stackOut.pop();
};

/**
 * @return {number}
 */
MyQueue.prototype.peek = function() {
    const valueOnTop = this.pop();
    this.stackOut.push(valueOnTop);
    return valueOnTop;
};

/**
 * @return {boolean}
 */
MyQueue.prototype.empty = function() {
    return !this.stackIn.length && !this.stackOut.length;
};

/** 
 * 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()
 */

时间复杂度: pop为O(n),其余为O(1) 空间复杂度: 总体为O(n)


225. Implement Stack using Queues

Implement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (pushtoppop, and empty).

Implement the MyStack class:

  • void push(int x) Pushes element x to the top of the stack.
  • int pop() Removes the element on the top of the stack and returns it.
  • int top() Returns the element on the top of the stack.
  • boolean empty() Returns true if the stack is empty, false otherwise.

Notes:

  • You must use only standard operations of a queue, which means that only push to backpeek/pop from frontsize and is empty operations are valid.
  • Depending on your language, the queue may not be supported natively. You may simulate a queue using a list or deque (double-ended queue) as long as you use only a queue's standard operations.

Example 1:

Input
["MyStack", "push", "push", "top", "pop", "empty"]
[[], [1], [2], [], [], []]
Output
[null, null, null, 2, 2, false]

Explanation
MyStack myStack = new MyStack();
myStack.push(1);
myStack.push(2);
myStack.top(); // return 2
myStack.pop(); // return 2
myStack.empty(); // return False

思路:

  • 要想清楚stack,queue, array (js)的操作, 因为要把array当成queue来模拟stack
  • 这就意味着只能用array的shift和push来模拟
  • push直接执行
  • pop,稍微有点技巧性,做this.queueOut.push(this.queueIn.shift())操作,直到queueIn剩下最后一个。最后一个元素就是栈顶元素,要return this.queueIn.shift()
  • top,pop出元素,再push给queueOut.
  • empty return !queueIn.length && !queueOut.length

代码:


var MyStack = function() {
    this.queueIn = [];
    this.queueOut = [];
};

/** 
 * @param {number} x
 * @return {void}
 */
MyStack.prototype.push = function(x) {
    this.queueIn.push(x);
};

/**
 * @return {number}
 */
MyStack.prototype.pop = function() {
    if (!this.queueIn.length) {
        [this.queueIn, this.queueOut] = [this.queueOut, this.queueIn];
    }
    
    while (this.queueIn.length > 1) {
        this.queueOut.push(this.queueIn.shift());
    }
    
    return this.queueIn.shift();
};

/**
 * @return {number}
 */
MyStack.prototype.top = function() {
    let valueOnTop = this.pop();
    this.queueOut.push(valueOnTop);
    return valueOnTop;
};

/**
 * @return {boolean}
 */
MyStack.prototype.empty = function() {
    return !this.queueIn.length && !this.queueOut.length;
};

/** 
 * 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()
 */

时间复杂度: pop为O(n2),其余为O(1) 空间复杂度: 总体为O(n)