LeetCode -- Implement Queue using Sracks

Question:

Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

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, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
    • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).

Analysis:

问题描述:请使用栈来实现一个队列。实现其基本功能:push(), pop(), peek(), empty()

注意:只能使用栈的标准功能:push(), peek(), pop(), size(), isEmpty(); 这取决于你使用的语言可能没有可供直接使用的栈,你可以使用链表或双向队列模拟一个栈,只要你仅仅使用栈的标准操作。

思路:使用2个栈模拟一个链表。两次后进先出==先进先出。因此,使用一个栈作为主栈,每次push()操作时直接执行,而当pop()或者peek()时,先依次移到第二个辅助栈中,然后取第二个栈的front指针指向的即可。而判断是否为空时,当且仅当两个栈都为空,队列才为空。

Answer:

class MyQueue {
        Stack<Integer> s1 = new Stack<Integer>();
        Stack<Integer> s2 = new Stack<Integer>();

        // Push element x to the back of queue.
        public void push(int x) {
            s1.push(x);
        }

        // Removes the element from in front of queue.
        public void pop() {
            if(!s2.isEmpty())
                s2.pop();
            else {
                while(!s1.isEmpty()) {
                        int i = s1.peek();
                        s1.pop();
                        s2.push(i);
                }
                s2.pop();
            }
        }

        // Get the front element.
        public int peek() {
            if(!s2.isEmpty())
                    return s2.peek();
            else {
                    while(!s1.isEmpty()) {
                        int i = s1.peek();
                        s1.pop();
                        s2.push(i);
                    }
                    return s2.peek();
            }
        }

        // Return whether the queue is empty.
        public boolean empty() {
            if(s1.isEmpty() && s2.isEmpty())
                    return true;
            return false;
        }
}
时间: 2024-10-28 23:13:51

LeetCode -- Implement Queue using Sracks的相关文章

[LeetCode] Implement Queue using Stacks 用栈来实现队列

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

leetcode Implement Queue using Stacks

Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether the queue is empty. Notes: You

LeetCode Implement Queue using Stacks (数据结构)

题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack1中的所有元素转移到stack2中,栈顶自然就是队头了,再弹出. (3)返回队头.与(2)一样. (4)是否为空.判断两个栈是否同时为空即可. 只要保证stack2为空时才可以将stack1转移到stack2中,就可以保证两边并不会产生混乱而出错. 1 class Queue { 2 /* 3 //

[LeetCode][JavaScript]Implement Queue using Stacks

Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether t

【LeetCode】232. Implement Queue using Stacks

Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whether t

Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

1. 232 Implement Queue using Stacks 1.1 问题描述 使用栈模拟实现队列.模拟实现如下操作: push(x). 将元素x放入队尾. pop(). 移除队首元素. peek(). 获取队首元素. empty(). 判断队列是否为空. 注意:只能使用栈的标准操作,push,pop,size和empty函数. 1.2 方法与思路 本题和用队列实现栈思路一样,设两个辅助栈stk1和stk2. push(x): 将x入栈stk1. pop(): 依次将stk1中的元素p

[CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列

3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Implement Queue using Stacks 用栈来实现队列.

leetcode_232——Implement Queue using Stacks(栈与队列)

Implement Queue using Stacks Total Accepted: 5687 Total Submissions: 16629My Submissions Question Solution Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from i

232. Implement Queue using Stacks &amp;&amp; 225. Implement Stack using Queues

232. Implement Queue using Stacks Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of queue. pop() -- Removes the element from in front of queue. peek() -- Get the front element. empty() -- Return whet