使用队列实现栈的下列操作:

  • push(x) -- 元素 x 入栈
  • pop() -- 移除栈顶元素
  • top() -- 获取栈顶元素
  • empty() -- 返回栈是否为空

注意:

  • 你只能使用队列的基本操作-- 也就是 push to backpeek/pop from frontsize, 和 is empty 这些操作是合法的。
  • 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。
  • 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。

超级慢的。

 class MyStack {
private LinkedList<Integer> list1 ;
private LinkedList<Integer> list2 ;
/** Initialize your data structure here. */
public MyStack() {
this.list1 = new LinkedList<Integer>();
this.list2 = new LinkedList<Integer>();
} /** Push element x onto stack. */
public void push(int x) {
list1.add(x);
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
int res = 0;
if(list1.size()==1){
return list1.poll();
}else{
while(list1.size()!=1){
list2.add(list1.poll());
}
res = list1.poll();
while(!list2.isEmpty()){
list1.add(list2.poll());
}
}
return res;
} /** Get the top element. */
public int top() {
int res = 0;
if(list1.size() == 1){
res = list1.peek(); }else{
while(list1.size()!=1){
list2.add(list1.poll());
}
res = list1.peek();
list2.add(list1.poll());
while(!list2.isEmpty()){
list1.add(list2.poll());
}
}
return res;
} /** Returns whether the stack is empty. */
public boolean empty() {
if(list1.isEmpty() && list2.isEmpty()){
return true;
}else{
return false;
}
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/

这个比较快:

 class MyStack {

     List<Integer> queue1=new LinkedList();
List<Integer> queue2=new LinkedList();
private int num=1;
/** Initialize your data structure here. */
public MyStack() { } /** Push element x onto stack. */
public void push(int x) {
if(num==1) {
queue1.add(x);
}else {
queue2.add(x);
}
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
if(num==1) {
while(queue1.size()!=1) {
queue2.add(queue1.remove(0));
}
num=2;
return queue1.remove(0);
}else {
while(queue2.size()!=1) {
queue1.add(queue2.remove(0));
}
num=1;
return queue2.remove(0);
}
} /** Get the top element. */
public int top() {
if(num==1) {
return queue1.get(queue1.size()-1);
}else {
return queue2.get(queue2.size()-1);
}
} /** Returns whether the stack is empty. */
public boolean empty() {
if(num==1) {
return queue1.isEmpty();
}else {
return queue2.isEmpty();
}
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/

最简单:

 class MyStack {
private Queue<Integer> data; public MyStack() {
data = new LinkedList<>();
} public void push(int x) {
data.offer(x);
//将队列中前面已经逆序的元素放在x元素后面,使得整体逆序
for (int i = 0; i < data.size() - 1; i++) {
data.offer(data.poll());
}
} public int pop() {
return data.poll();
} public int top() {
return data.peek();
} public boolean empty() {
return data.isEmpty();
}
} /**
* Your MyStack object will be instantiated and called as such:
* MyStack obj = new MyStack();
* obj.push(x);
* int param_2 = obj.pop();
* int param_3 = obj.top();
* boolean param_4 = obj.empty();
*/

2019-03-04 23:00:41

LeetCode--255--用队列实现栈(java版)的更多相关文章

  1. 两个栈实现队列+两个队列实现栈----java

                                               两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...

  2. Java实现 LeetCode 225 用队列实现栈

    225. 用队列实现栈 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() – 获取栈顶元素 empty() – 返回栈是否为空 注意: 你只能使 ...

  3. 领扣(LeetCode)用队列实现栈 个人题解

    使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作 ...

  4. [LeetCode] 226. 用队列实现栈

    题目链接: https://leetcode-cn.com/problems/implement-stack-using-queues 难度:简单 通过率:59.9% 题目描述: 使用队列实现栈的下列 ...

  5. [Leetcode]225. 用队列实现栈 、剑指 Offer 09. 用两个栈实现队列

    ##225. 用队列实现栈 如题 ###题解 在push时候搞点事情:push时入队1,在把队2的元素一个个入队1,再交换队2和队1,保持队1除pushguocheng 始终为空. ###代码 cla ...

  6. LeetCode--155--最小栈(java版)

    设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈. push(x) -- 将元素 x 推入栈中. pop() -- 删除栈顶的元素. top() -- 获取栈顶元素. ...

  7. 《剑指offer》面试题21 包含min函数的栈 Java版

    (min函数的作用是返回栈内最小值) 首先这个栈要具有普通栈所具有的push()和pop()方法,那么内部一定包含一个Stack.至于还要能实现min函数,而且还是在O(1)时间复杂度内,我们不得不考 ...

  8. 剑指offer第二版面试题9:用两个队列实现栈(JAVA版)

    题目:用两个队列实现栈. 分析:通过一系列的栈的压入和弹出操作来分析用队列模拟一个栈的过程,如图所示,我们先往栈内压入一个元素a.由于两个队列现在都是空,我们可以选择把a插入两个队列中的任一个.我们不 ...

  9. 【LeetCode题解】225_用队列实现栈(Implement-Stack-using-Queues)

    目录 描述 解法一:双队列,入快出慢 思路 入栈(push) 出栈(pop) 查看栈顶元素(peek) 是否为空(empty) Java 实现 Python 实现 解法二:双队列,入慢出快 思路 入栈 ...

随机推荐

  1. Selenium Java Selection的使用

    用于向具有drop-down的选择框中输入内容 new Select(new ChromeDriver().findElement(By.cssSelector(" ..."))) ...

  2. topcoder srm 445 div1

    problem1 link 这个的结论是只需要考虑坐标是整数或者是整数.5,比如(2.5,3),(4,3.5),(1.5,4.5)这样的时候.这个详细证明起来应该挺麻烦的.这里有一些讨论. probl ...

  3. MVC 自定义特性(验证用户登录session是否已经过期)

    新建一个类 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] publ ...

  4. uniGUI试用笔记(十一)

    最近研究了一下UniGUI的TuniDBGrid,记录一下免得忘记了. TuniDBGrid的重要属性包括: 1.列—TUniDBGridColumns和TUniDBGridColumn 每个列对象( ...

  5. CF 1087解题报告

    cf解题报告 记录一下吧 做出:T1 rating :-97 想起几个月前做不出T1还是有点小搞笑呀2333 T1 双指针+特判 T2 发现k特别小,枚举剩余系 还要判断是否是能被n整除 移项发现可以 ...

  6. Spring-Cache 注解 @Cacheable,@CachePut , @CacheEvict

    1.自动生成key @Bean public KeyGenerator keyGenerator() { return new KeyGenerator() { @Override public Ob ...

  7. 剥开比原看代码09:通过dashboard创建密钥时,前端的数据是如何传到后端的?

    作者:freewind 比原项目仓库: Github地址:https://github.com/Bytom/bytom Gitee地址:https://gitee.com/BytomBlockchai ...

  8. eclipse创建maven web项目工程步骤示例

    参考链接:https://www.cnblogs.com/noteless/p/5213075.html 需求表均同springmvc案例 此处只是使用maven 注意,以下所有需要建立在你的ecli ...

  9. Linux中.rar文件解压

    1. 下载: https://www.rarlab.com/download.htm 我下载的是RAR 5.61 for Linux x64 2. 安装: 解压:tar -zxvf rarlinux- ...

  10. [0413] FFTSHIFT的四种写法

    FFTSHIFT的四种写法 前言 matlab说,"你读过书,--我便考你一考.fftshift的函数,怎样写的?"我想,讨饭一样的人,也配考我么?便回过脸去,不再理会.matla ...