Implement Queue using Stacks 解答

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).

Solution

 1 class MyQueue {
 2     Stack<Integer> stack1;
 3     Stack<Integer> stack2;
 4
 5     public MyQueue() {
 6         stack1 = new Stack<Integer>();
 7         stack2 = new Stack<Integer>();
 8     }
 9
10     // Push element x to the back of queue.
11     public void push(int x) {
12         stack1.push(x);
13     }
14
15     // Removes the element from in front of queue.
16     public void pop() {
17         stack2 = new Stack<Integer>();
18         int length = stack1.size();
19         while (length > 1) {
20             stack2.push(stack1.pop());
21             length--;
22         }
23         stack1.pop();
24         length = stack2.size();
25         while (length > 0) {
26             stack1.push(stack2.pop());
27             length--;
28         }
29     }
30
31     // Get the front element.
32     public int peek() {
33         stack2 = new Stack<Integer>();
34         int current = 0;
35         int length = stack1.size();
36         while (length > 0) {
37             current = stack1.pop();
38             stack2.push(current);
39             length--;
40         }
41         length = stack2.size();
42         while (length > 0) {
43             stack1.push(stack2.pop());
44             length--;
45         }
46         return current;
47     }
48
49     // Return whether the queue is empty.
50     public boolean empty() {
51         return stack1.empty();
52     }
53 }
时间: 2024-08-01 19:06:56

Implement Queue using Stacks 解答的相关文章

【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(栈与队列)

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

[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 和 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

Lintcode: Implement Queue by Stacks 解题报告

Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As the title described, you should only use two stacks to implement a queue's actions. The queue should support push(element), pop() and top() where pop is

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:

126. Implement Queue using Stacks

题目: Implement the following operations of a queue using stacks. 使用堆栈实现队列的以下操作. push(x) -- Push element x to the back of queue. 将元素x推送到队列的后面. pop() -- Removes the element from in front of queue.从队列前面删除元素. peek() -- Get the front element.获取前面的元素. empty

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 whether the queue is empty. Notes: You