LeetCode(23)-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 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).
思路:
- 这道题的意思是用堆栈(stack)来实现队列(quene)
- 可以用两个stack,s1和s2来实现,s1用来整体的存储,s2用来缓冲倒叙,首先判断s2是否为空,为空,把s1弹出到s2,否则s2.pop()
代码:
class MyQueue {
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
// 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{
if(s1.isEmpty()){
return;
}else{
while(!s1.isEmpty()){
int tmp = s1.pop();
s2.push(tmp);
}
s2.pop();
}
}
}
// Get the front element.
public int peek() {
if(!s2.isEmpty()){
return s2.peek();
}else{
if(s1.isEmpty()){
return -1;
}else{
while(!s1.isEmpty()){
int tmp = s1.pop();
s2.push(tmp);
}
return s2.peek();
}
}
}
// Return whether the queue is empty.
public boolean empty() {
return(s1.isEmpty() && s2.isEmpty());
}
}
LeetCode(23)-Implement Queue using Stacks的更多相关文章
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- [LeetCode] 232. Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- (easy)LeetCode 232.Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
- LeetCode 232 Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- leetCode(37):Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Java for LeetCode 232 Implement Queue using Stacks
Stack<Integer> stack=new Stack<Integer>(); public void push(int x) { stack.push(x); } // ...
- Leetcode 232 Implement Queue using Stacks STL
本题用两个栈实现队列,用栈的基本操作去实现队列的所有基本操作push(),pop(),peek()以及empty() sa作为输入栈,sb作为输出栈,将sa输入元素的反转过来放到sb中 push与sa ...
- LeetCode 232 Implement Queue using Stacks 两个栈实现队列
class MyQueue { public: /** Initialize your data structure here. */ MyQueue() { } /** Push element x ...
随机推荐
- Dynamics CRM 2015Online Update1 new feature之 插件跟踪日志
在最新的CRM2015Online Update1版本中加入了一个新功能-插件跟踪日志,与其说是新功能更应该说是对原有功能的加强,因为ITracingService这个接口在2013中已经引入了, ...
- Android简易实战教程--第二话《两种进度条》
点击按钮模拟进度条下载进度,"下载"完成进度条消失. 代码如下: xml: <?xml version="1.0" encoding="utf- ...
- EasyUI表单验证,自定义插件验证,自定义js插件验证,远程验证,常见手机号,中英文,qq等验证规则验证
{ field : 'startPort', title : "起始端口", editor: "text", width : 50, editor: { ...
- Uva - 1589 - Xiangqi
Xiangqi is one of the most popular two-player board games in China. The game represents a battle bet ...
- UNIX网络编程——基本TCP套接字编程
一.基于TCP协议的网络程序 下图是基于TCP协议的客户端/服务器程序的一般流程: 服务器调用socket().bind().listen()完成初始化后,调用accept()阻塞等待,处于监听端口的 ...
- Java进阶(六)Java反射机制可恶问题NoSuchFieldException
作为一种重要特性,Java反射机制在很多地方会用到.在此做一小结,供朋友们参考. 首先从一个问题开始着手. 可恶的问题又来了,NoSuchFieldException,如下图所示: 完全不知道这个qu ...
- [python] Start a http server
If you want to start a simple httpserver on your windows, you may choose python.simpleHTTPServer mod ...
- Guava 教程1-使用 Google Collections,Guava,static imports 编写漂亮代码
原文出处: oschina (API:http://ifeve.com/category/framework/guava-2/ JAR DOC Source 链接:http://pan.baidu.c ...
- Libgdx1.6.2发布,跨平台游戏开发框架
原文地址:www.libgdx.cn [1.6.2] API更改:TiledMapImageLayer位置由整型改为浮点类型. API更改:添加GLFrameBuffer 和 FrameBufferC ...
- MFC的两个问题
1.启动WinApp的时候,报 ASSERT(AfxGetThread() == NULL)错误依赖的MFC DLL工程设置里面加上_USRDLL2. MFC误报内存泄露全局对象释放的问题,添加mfc ...