Java [Leetcode 225]Implement Stack using Queues
题目描述:
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
, andis 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).
解题思路:
代码如下:
class MyStack {
Queue<Integer> queue = new LinkedList<Integer>();
// Push element x onto stack.
public void push(int x) {
queue.offer(x);
} // Removes the element on top of the stack.
public void pop() {
int size = queue.size();
for(int i = 1; i < size; i++)
queue.offer(queue.poll());
queue.poll();
} // Get the top element.
public int top() {
int size = queue.size();
for(int i = 1; i < size; i++)
queue.offer(queue.poll());
int head = queue.poll();
queue.offer(head);
return head;
} // Return whether the stack is empty.
public boolean empty() {
return queue.isEmpty();
}
}
Java [Leetcode 225]Implement Stack using Queues的更多相关文章
- Java for LeetCode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- (easy)LeetCode 225.Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- Leetcode 225 Implement Stack using Queues
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- LeetCode 225 Implement Stack using Queues 用队列实现栈
1.两个队列实现,始终保持一个队列为空即可 class MyStack { public: /** Initialize your data structure here. */ MyStack() ...
- leetcode 155. Min Stack 、232. Implement Queue using Stacks 、225. Implement Stack using Queues
155. Min Stack class MinStack { public: /** initialize your data structure here. */ MinStack() { } v ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- jquery获取标签内容,编辑内容
一.获取页面元素 三种方式获取页面中元素的内容. input标签使用:.val()获取 标签下的html及文本内容:.html() 仅获取标签下的纯文本内容:.text() <head> ...
- 【转载】C++针对ini配置文件读写大全
http://blog.csdn.net/hgy413/article/details/6666374# ini文件(Initialization file),这种类型的文件中通常存放的是一个程序的初 ...
- springMVC 简单事例
本帖最后由 悲观主义者一枚 于 2015-1-31 17:55 编辑 使用SpringMvc开发Android WebService入门教程1.首先大家先创建一个JavaWeb项目2.然后加入Spri ...
- libvirt编译报错
virsh # list --all错误:连接到管理程序失败错误:无效的连接错误:将插槽连接到 '/usr/local/var/run/libvirt/libvirt-sock' 失败: 没有那个文件 ...
- javascript表格的添加和删除
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...
- JAVASCRIPT中的作用域和原型链,应该算是难点了,要好好多学学,练练
今天初六,要上班啦... JAVASCRIPT,看来是丢不了了.. http://www.dengdeng90.com/wordpress/?p=241 http://www.cnblogs.com/ ...
- CLR 读书笔记
http://www.cnblogs.com/wang_yb/tag/CLR%20via%20C%23%E7%AC%94%E8%AE%B0/
- hdu2544 最短路
题目:http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路径DIJKSTRA #include<stdio.h> #include<m ...
- U盘安装Win7 64位
试了好几遍,失败了的就不说了,直接记下成功的方案,方便下次. 方法为:用UltraISO刻镜像文件到U盘,然后U盘启动安装. 具体如下: 刻u盘之前一定要验证iso镜像的完整性啊(可以用文件校验工具与 ...
- OpenStack重启之后,dashboard登录不上去
ubuntu 12.04装好openstack之后,安装成功,终端打出如下信息: Horizon is now available at http://192.168.0.2/Keystone is ...