(easy)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 emptyoperations 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).
Update (2015-06-11):
The class name of the Java function had been updated to MyStack instead of Stack.
Credits:
Special thanks to @jianchao.li.fighter for adding this problem and all test cases.
解析:java 中,Queue接口可使用的函数有 offer(E e):添加到队列尾 peek():查看队列头 poll():删除队头元素 remove()移除队头元素
另外:Deque接口中的push(),pop()操作均是在队头操作。
代码如下:
class MyStack {
// Push element x onto stack.
Queue<Integer> q1=new LinkedList<Integer>();
Queue<Integer> q2=new LinkedList<Integer>();
public void push(int x) {
q1.offer(x);
}
// Removes the element on top of the stack.
public void pop() {
while(q1.size()>1) q2.offer(q1.poll());
q1.poll();
Queue<Integer> q=q1;
q1=q2;
q2=q;
}
// Get the top element.
public int top() {
while(q1.size()>1) q2.offer(q1.poll());
int x=q1.poll();
q2.offer(x);
Queue<Integer>q=q1;
q1=q2;
q2=q;
return x;
}
// Return whether the stack is empty.
public boolean empty() {
return q1.isEmpty();
}
}
运行结果:

(easy)LeetCode 225.Implement Stack using Queues的更多相关文章
- LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)
翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...
- [LeetCode] 225. Implement Stack using Queues 用队列来实现栈
Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...
- 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 ...
- Java [Leetcode 225]Implement Stack using Queues
题目描述: Implement the following operations of a stack using queues. push(x) -- Push element x onto sta ...
- Leetcode 225 Implement Stack using Queues STL
用两个队列去实现栈,这里我使用了队列数组q[2],在所有的过程中保证一个队列是空的 push时插入到空的队列中,然后将队列中的元素移到另一个队列中 pop时从不空的队列中pop() peek时从不空的 ...
- 【easy】225. Implement Stack using Queues
用队列实现栈.这个实现方法十分的简单,就是在push这一步的时候直接变成逆序. class MyStack { private: queue<int> q; queue<int> ...
- 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 ...
随机推荐
- oracle row_number()
要求查询每个用户对应的最大样品信息,忽然想到ms sql提供过 row_number() over(partition by 列 order by 列 desc),那么oracle可能也存在, 我的表 ...
- 彻底解决ASP.NET MVC 3 404错误码返回302的问题
转自:http://blog.csdn.net/mycloudke/article/details/9746333 404状态码:,意味着当在页面上显示用户点击不存在,提高用户体验度,搜索引擎会放弃这 ...
- CA接口测试类
package com.creditharmony.adapter.testCase.ca; import org.junit.Test; import com.alibaba.druid.util. ...
- CSS控制checkbox样式
原文地址:http://www.xiumu.org/technology/style-checkboxes-with-css.shtml#comments Checkbox复选框是一个可能每一个网站都 ...
- SPOJ #692. Fruit Farm
Another palindrome related problem. Actually nothing too theoretical here, but please keep following ...
- POJ 3709 K-Anonymous Sequence
题目大意:将一个升序的,有N个元素的序列,分组.要求每组的元素不少于K个,计算出组内各元素与最小元素的之差的和,将每组的这个值加起来,其和要最小. N<500000,K<N 分析: dp[ ...
- PHP解码unicode编码中文字符代码示例
在抓取某网站数据,结果在数据包中发现了一串编码的数据:"......\u65b0\u6d6a\u5fae\u535a......", 这其实是中文被unicode编码后了的数据,想 ...
- 8. redis的主从复制和sentinal
一. redis主从复制(读写分离) redis的主从复制分为两类节点:1个master和多个slave,master进行读写操作,slav进行只读操作 启动步骤: 主节点照常启动,slave节点启动 ...
- easy dp
1.将一堆正整数分为2组,要求2组的和相差最小. //File Name: nod1007.cpp //Author: long //Mail: 736726758@qq.com //Created ...
- dede图片横向滚动
<div id=demo style="overflow:hidden; width:960px;" > <table border=0 align=" ...