【Leetcode】Implement Queue using Stacks
来源:互联网 发布:fotor mac 破解 编辑:程序博客网 时间:2024/05/01 16:33
【题目】
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.
- You must use only standard operations of a stack -- which means only
push to top
,peek/pop from top
,size
, andis 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).
【思路】
I have one input stack, onto which I push the incoming elements, and one output stack, from which I peek/pop. I move elements from input stack to output stack when needed, i.e., when I need to peek/pop but the output stack is empty. When that happens, I move all elements from input to output stack, thereby reversing the order so it's the correct order for peek/pop.
The loop in peek
does the moving from input to output stack. Each element only ever gets moved like that once, though, and only after we already spent time pushing it, so the overall amortized cost for each operation is O(1).
【代码】
class MyQueue { Stack<Integer> input = new Stack(); Stack<Integer> output = new Stack(); public void push(int x) { input.push(x); } public void pop() { peek(); output.pop(); } public int peek() {
//哇塞。。。这里简直太巧妙了。 if (output.empty()) while (!input.empty()) output.push(input.pop()); return output.peek(); } public boolean empty() { return input.empty() && output.empty(); }}
class MyQueue { // Push element x to the back of queue. private Stack<Integer> one = new Stack<>(); private Stack<Integer> two = new Stack<>(); private int count = 0; public void push(int x) { one.push(x); count++; } // Removes the element from in front of queue. public void pop() { int tmp = 0; if(empty()) return; for(int i = 0 ; i<count - 1;i++){ tmp = one.pop(); two.push(tmp); } int result = one.pop(); while(!two.isEmpty()){ tmp = two.pop(); one.push(tmp); } count--; } // Get the front element. public int peek() { int tmp = 0; if(empty()) return 0; for(int i = 0 ; i<count - 1;i++){ tmp = one.pop(); two.push(tmp); } int result = one.peek(); while(!two.isEmpty()){ tmp = two.pop(); one.push(tmp); } return result; } // Return whether the queue is empty. public boolean empty() { return (count == 0); }}
0 0
- leetcode :Implement Queue using Stacks
- [LeetCode] Implement Queue using Stacks
- LeetCode Implement Queue using Stacks
- LeetCode || Implement Queue using Stacks
- leetcode Implement Queue using Stacks
- Leetcode Implement Queue using Stacks
- [LeetCode]Implement Queue using Stacks
- 【Leetcode】Implement Queue using Stacks
- LeetCode Implement Queue using Stacks
- Leetcode: Implement Queue using Stacks
- [leetcode] Implement Queue using Stacks
- 【leetcode】Implement Queue using Stacks
- [Leetcode]Implement Queue using Stacks
- [leetcode] Implement Queue using Stacks
- LeetCode-Implement Queue using Stacks
- LeetCode Implement Queue using Stacks
- Leetcode: Implement Queue using Stacks
- LeetCode:Implement Queue using Stacks
- BladeCenter H-bnt交换机的配置
- R语言ggplot包之数据分布汇总
- C#高级编程----小结
- AsyncHttpClient介绍
- [2015年7月][20~25][poj2823, 项目问题解决]
- 【Leetcode】Implement Queue using Stacks
- hibernate 3中的缓存小结
- android 清空指定文件夹
- Linux的常用命令(1) - 指定运行级别
- 1080 两个数的平方和
- 利用LLVM,Clang制作自己的编译器
- Java里不改变内存的指向而改变String的值
- POJ-3620
- BC400-Unit1:Flow of an ABAP Program