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 pop the first(a.k.a front) element in the queue.

Both pop and top methods should return the value of first element.

样例

For push(1), pop(), push(2), push(3), top(), pop(), you should return 1, 2 and 2

挑战

implement it by two stacks, do not use any other data structure and push, pop and top should be O(1) by AVERAGE.

SOLUTION 1:

使用两个栈,stack1和stack2。

http://www.ninechapter.com/problem/49/

对于Queue的操作对应如下:

Queue.Push:

push到Stack1

Queue.Pop:

如果Stack2非空,Stack2.pop

否则将Stack1中的所有数pop到Stack2中(相当于顺序颠倒了放入),然后Stack2.pop()

每个数进出Stack1和Stack2各1次,所以两个操作的均摊复杂度均为O(1)

 1 public class Solution {
 2     private Stack<Integer> stack1;
 3     private Stack<Integer> stack2;
 4
 5     public Solution() {
 6        // do initialization if necessary
 7        stack1 = new Stack<Integer>();
 8        stack2 = new Stack<Integer>();
 9     }
10
11     public void push(int element) {
12         // write your code here
13         stack1.push(element);
14     }
15
16     public int pop() {
17         // write your code here
18         if (stack2.isEmpty()) {
19             while (!stack1.isEmpty()) {
20                 stack2.push(stack1.pop());
21             }
22         }
23
24         return stack2.pop();
25     }
26
27     public int top() {
28         // write your code here
29         // write your code here
30         if (stack2.isEmpty()) {
31             while (!stack1.isEmpty()) {
32                 stack2.push(stack1.pop());
33             }
34         }
35
36         return stack2.peek();
37     }
38 }

GITHUB:

https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/stack/StackQueue.java

时间: 2024-08-21 09:46:28

Lintcode: Implement Queue by 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

【一天一道LeetCode】#232. Implement Queue using Stacks

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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

LeetCode 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:

Java [Leetcode 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. Note

【LeetCode OJ 232】Implement Queue using Stacks

题目链接:https://leetcode.com/problems/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 fro