Leetcode 225,232 用两个队列实现栈,用两个栈实现队列
2016-01-11 14:26
381 查看
Leetcode 225,232 用两个队列实现栈,用两个栈实现队列
232 Implement Queue using Stacks My Submissions QuestionTotal Accepted: 29497 Total Submissions: 86968 Difficulty: Easy
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 top, peek/pop from top, size, 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).**
s1,s2两个栈,出队列时如果s2为空,将s1的元素全部Pop到s2再从s2 Pop。否则直接s2 Pop。
入队列时直接s1 Push
代码
public class Queue { Stack<int> s1 = new Stack<int>(); Stack<int> s2 = new Stack<int>(); // Push element x to the back of queue. public void Push(int x) { s1.Push(x); } // Removes the element from front of queue. public void Pop() { if(s2.Count==0) { while(s1.Count>0) s2.Push(s1.Pop()); } s2.Pop(); } // Get the front element. public int Peek() { if(s2.Count==0) { while(s1.Count>0) s2.Push(s1.Pop()); } return s2.Peek(); } // Return whether the queue is empty. public bool Empty() { return s1.Count==0 && s2.Count==0; } }
**225 Implement Stack using Queues My Submissions Question
Total Accepted: 27669 Total Submissions: 91210 Difficulty: Easy
Implement the following operations of a stack using queues.
push(x) – Push element x onto stack.
pop() – Removes the element on top of the stack.
top() – Get the top element.
empty() – Return whether the stack is empty.
Notes:
You must use only standard operations of a queue – which means only push to back, peek/pop from front, size, and is empty operations are valid.
Depending on your language, queue may not be supported natively. You may simulate a queue by using a list or deque (double-ended queue), as long as you use only standard operations of a queue.
You may assume that all operations are valid (for example, no pop or top operations will be called on an empty stack).**
感觉这个方法比较笨,不知道有没有更好的。始终保持一个队列为空。入队列入非空的队列。出队列时把非空队列的除最后一个外全部转移到空的队列,然后出非空队列最后一个。
代码
public class Stack { Queue q1=new Queue(),q2=new Queue(); // Push element x onto stack. public void Push(int x) { if(q1.Count!=0) q1.Enqueue(x); else if(q2.Count!=0) q2.Enqueue(x); else q1.Enqueue(x); } // Removes the element on top of the stack. public void Pop() { Queue q = q1.Count==0?q2:q1; Queue qq = q1.Count==0?q1:q2; while(q.Count>1) { qq.Enqueue(q.Dequeue()); } q.Dequeue(); } // Get the top element. public int Top() { Queue q = q1.Count==0?q2:q1; Queue qq = q1.Count==0?q1:q2; while(q.Count>1) { qq.Enqueue(q.Dequeue()); } int ret = (int)q.Peek(); qq.Enqueue(q.Dequeue()); return ret; } // Return whether the stack is empty. public bool Empty() { return q1.Count==0 && q2.Count==0; } }
相关文章推荐
- Performance Tuning guide 翻译 || Performance Tuning Guide 11G中新增特性
- bootstrap学习记录
- JS的String对象方法总结
- hive 建表语句
- [synergy] 使用教程· 多台电脑共享键盘和鼠标
- Mysql IO 内存方面的优化
- html点击小图查看大图
- JPEG图像的压缩解压 ljpeg
- ♥蓝桥杯 ALGO-116 最大的算式
- free-scada
- 递归神经网络的启示和神奇的效果
- 高通专利
- [jQuery] DOM & CSS
- java程序员必须掌握的 正则表达式
- iOS 删除沙盒文件中某一个视频
- Unity3D Vector3.Dot 点乘 与 Vector3.Cross 叉乘
- freeRadius配置2
- Guava包学习--Multiset
- c#大圣之路——c#获取DataGrid中选中行某一列的数据,传到其他页面,实现页面间数据传递
- 一鍵分享(優化后)