链接

https://leetcode-cn.com/problems/implement-stack-using-queues/

思路

首先演示push()操作;
将元素依次进入队1,进入时用top元素保存当前进入的元素;
如下图:

                                  push操作的演示

然后演示pop()操作;
先将除队1中的最后一个元素出队并进入队2,入队2时用top存储入队元素;
再将队列1和队列2进行互换即可。
如下图:

                                    pop操作的演示

代码

import java.util.LinkedList;
import java.util.Queue; class MyStack {     private Queue<Integer> queue_1 = new LinkedList<>();
    private Queue<Integer> queue_2 = new LinkedList<>();
    private int top;
    /** Initialize your data structure here. */
    public MyStack() {     }     /** Push element x onto stack. */
    public void push(int x) {
        queue_1.add(x);
        top = x;
    }     /** Removes the element on top of the stack and returns that element. */
    public int pop() {
        if(empty()){
            throw new RuntimeException("MyStack空了!");
        }
        while(queue_1.size() > 1){
            top = queue_1.remove();
            queue_2.add(top);
        }
        int last_ele = queue_1.remove();
        Queue<Integer> queue_temp = queue_1;
        queue_1 = queue_2;
        queue_2 = queue_temp;
        return last_ele;
    }     /** Get the top element. */
    public int top() {
        if(empty()){
            throw new RuntimeException("MyStack空了!");
        }
        return top;
    }     /** Returns whether the stack is empty. */
    public boolean empty() {
        return queue_1.isEmpty() && queue_2.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();
 */

欢迎关注

扫下方二维码即可关注:,微信公众号:code随笔

                               

LeetCode 225题用队列实现栈(Implement Stack using Queues) Java语言求解的更多相关文章

  1. LeetCode 225:用队列实现栈 Implement Stack using Queues

    题目: 使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 Implement th ...

  2. [Swift]LeetCode225. 用队列实现栈 | Implement Stack using Queues

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  3. Leetcode 225 两个队列实现栈

    Implement the following operations of a stack using queues. push(x) -- Push element x onto stack. po ...

  4. 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 ...

  5. 【LeetCode】232 & 225 - Implement Queue using Stacks & Implement Stack using Queues

    232 - Implement Queue using Stacks Implement the following operations of a queue using stacks. push( ...

  6. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  7. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  8. Implement Queue by Two Stacks & Implement Stack using Queues

    Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...

  9. LeetCode 225 Implement Stack using Queues(用队列来实现栈)(*)

    翻译 用队列来实现栈的例如以下操作. push(x) -- 将元素x加入进栈 pop() -- 从栈顶移除元素 top() -- 返回栈顶元素 empty() -- 返回栈是否为空 注意: 你必须使用 ...

随机推荐

  1. Oscar的数理统计笔记本

    目录 Random Variable discrete distribution: continuous distribution: 统计量 现代统计学时期: Survey sampling Rand ...

  2. Debian8.8解决双系统访问windows磁盘时,有时能成功挂载,有时不能成功挂载的情况

    1.确保在debian下安装了挂载工具.2.进入windows关闭windows快速启动功能,关闭办法是控制面板,在电源管理中,选择关闭盖子的功能,点击“更改不能更改的选项”,去掉快速启动的钩,重启进 ...

  3. vue结合element实现自定义上传图片、文件

    参考了很多文献,感谢各位帖子,所以也想把自己遇到不会的东西分享出来,菜鸟一枚大家一进步!

  4. top 命令中的VIRT,RES,SHR ,MEM区别

    VIRT 表示进程的虚拟(地址)空间大小,其包含进程实际使用的大小(申请的堆栈), 使用mmap映射的大小,包括外设RAM, 还有映射到本进程的文件(例如动态库),还有进程间的共享内存.所以VIRT ...

  5. Trying to find the anti-derivative of $\tan x$ unsuccessfully by using Euler's formula

    We know that$$\tan t=\frac{e^{it}-e^{-it}}{i(e^{it}+e^{-it})}=\frac{e^{2i    t}+1-2}{i(e^{2it}+1)}=- ...

  6. The website is API(2)

    一.Beautifu Soup库 from bs4 import BeautifulSoup soup = BeautifulSoup(demo,"html.parser") Ta ...

  7. J - Association of Cats and Magical Lights Kattis - magicallights (树状数组+dfs序)

    Rar the Cat((™)) is the founder of ACM (Association of Cats and Magical Lights). This is the story o ...

  8. 变得“不正经”的CES,竟然越来越好玩了

    在所有科技界的展会中,国人最熟悉的当属CES.作为科技行业的风向指示标,CES一直在扮演着重要的潮流指引者角色.不过,现在的CES似乎变得越来越"不正经"了!原本CES是国际消费类 ...

  9. java中==和equals

    /** * @author zhaojiatao * @date 2018/7/19 */ public class equalsLearn { public static void main(Str ...

  10. 吴裕雄--天生自然python学习笔记:python下载安装各种模块的whl文件网址

    python下载安装各种模块的whl文件网址:https://www.lfd.uci.edu/~gohlke/pythonlibs/#lxml