这是悦乐书的第195次更新,第201篇原创

01 看题和准备

今天介绍的是LeetCode算法题中Easy级别的第57题(顺位题号是232)。使用栈实现队列的以下操作。

push(x) - 将元素x推送到队列的后面。

pop() - 从队列前面删除元素。

peek() - 获取前面的元素。

empty() - 返回队列是否为空。

例如:

MyQueue queue = new MyQueue();

queue.push(1);

queue.push(2);

queue.peek(); //返回1

queue.pop(); //返回1

queue.empty(); //返回false

本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

02 第一种解法

队列的特性是先进先出,而栈的特性是先进后去,在使用栈进行队列的出队列和队顶操作时,需要借助另外一个栈来进行反转然后再还原,而入队列的操作还是无需特殊处理。

  1. class MyQueue {
  2. private Stack<Integer> stack;
  3. /** Initialize your data structure here. */
  4. public MyQueue() {
  5. stack = new Stack<Integer>();
  6. }
  7. /** Push element x to the back of queue. */
  8. public void push(int x) {
  9. stack.push(x);
  10. }
  11. /** Removes the element from in front of queue and returns that element. */
  12. public int pop() {
  13. Stack<Integer> temp = new Stack<Integer>();
  14. while (!stack.isEmpty()) {
  15. temp.push(stack.pop());
  16. }
  17. int tem = temp.pop();
  18. while (!temp.isEmpty()) {
  19. stack.push(temp.pop());
  20. }
  21. return tem;
  22. }
  23. /** Get the front element. */
  24. public int peek() {
  25. Stack<Integer> temp = new Stack<Integer>();
  26. while (!stack.isEmpty()) {
  27. temp.push(stack.pop());
  28. }
  29. int tem = temp.peek();
  30. while (!temp.isEmpty()) {
  31. stack.push(temp.pop());
  32. }
  33. return tem;
  34. }
  35. /** Returns whether the queue is empty. */
  36. public boolean empty() {
  37. return stack.isEmpty();
  38. }
  39. }

03 第二种解法

此解法和上面的第一种解法正好相反,是在入队列的时候,借助另外一个栈来进行反转操作,而出队列和获取队列顶的操作可以直接使用栈的方法,无需特殊处理。

  1. class MyQueue2 {
  2. Stack<Integer> stack;
  3. /** Initialize your data structure here. */
  4. public MyQueue2() {
  5. stack = new Stack<Integer>();
  6. }
  7. /** Push element x to the back of queue. */
  8. public void push(int x) {
  9. Stack<Integer> temp=new Stack<>();
  10. while(!stack.isEmpty()){
  11. temp.push(stack.pop());
  12. }
  13. temp.push(x);
  14. while(!temp.isEmpty()){
  15. stack.push(temp.pop());
  16. }
  17. }
  18. /** Removes the element from in front of queue and returns that element. */
  19. public int pop() {
  20. return stack.pop();
  21. }
  22. /** Get the front element. */
  23. public int peek() {
  24. int a = stack.pop();
  25. stack.push(a);
  26. return a;
  27. }
  28. /** Returns whether the queue is empty. */
  29. public boolean empty() {
  30. return stack.isEmpty();
  31. }
  32. }

04 第三种解法

此解法使用两个栈s1、s2来实现队列的相关操作。

入队列时,如果s2不为空,那么先把s2中的元素pop出来在push进s1,然后才去将当前要插入的数据push进s1。

出队列时,如果s2为空,即此前没有进行出队列操作或者获取队列顶的操作,那么就需要将s1反转,即将s1的元素pop出来,然后push进s2中,此时再返回s2的pop操作即可。如果s2不为空,即说明上一次操作不是入队列,而是出队列或获取队列顶的操作,直接返回s2的pop操作即可。

获取队列顶时,和入队列操作时的判断一致,只不过最后返回s2的peek操作即可。

  1. class MyQueue3 {
  2. private Stack<Integer> s1;
  3. private Stack<Integer> s2;
  4. /** Initialize your data structure here. */
  5. public MyQueue3() {
  6. s1 = new Stack<Integer>();
  7. s2 = new Stack<Integer>();
  8. }
  9. /** Push element x to the back of queue. */
  10. public void push(int x) {
  11. while(!s2.empty()) {
  12. s1.push(s2.pop());
  13. }
  14. s1.push(x);
  15. }
  16. /** Removes the element from in front of queue and returns that element. */
  17. public int pop() {
  18. if (s2.empty()) {
  19. while (!s1.empty()) {
  20. s2.push(s1.pop());
  21. }
  22. }
  23. return s2.pop();
  24. }
  25. /** Get the front element. */
  26. public int peek() {
  27. if (s2.empty()) {
  28. while (!s1.empty()) {
  29. s2.push(s1.pop());
  30. }
  31. }
  32. return s2.peek();
  33. }
  34. /** Returns whether the queue is empty. */
  35. public boolean empty() {
  36. return (s1.empty() && s2.empty());
  37. }
  38. }

05 第四种解法

在入队列时,始终往第一位插入元素,而其他的出队列、获取队列的顶、判空这些操作都可以直接使用栈的方法,而无需重新实现。

  1. class MyQueue4 {
  2. private Stack<Integer> stack;
  3. /** Initialize your data structure here. */
  4. public MyQueue4() {
  5. stack = new Stack<Integer>();
  6. }
  7. /** Push element x to the back of queue. */
  8. public void push(int x) {
  9. stack.add(0, x);
  10. }
  11. /** Removes the element from in front of queue and returns that element. */
  12. public int pop() {
  13. return stack.pop();
  14. }
  15. /** Get the front element. */
  16. public int peek() {
  17. return stack.peek();
  18. }
  19. /** Returns whether the queue is empty. */
  20. public boolean empty() {
  21. return stack.isEmpty();
  22. }
  23. }

06 小结

算法专题目前已连续日更超过一个月,算法题文章57+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

LeetCode算法题-Implement Queue Using Stacks(Java实现)的更多相关文章

  1. LeetCode算法题-Letter Case Permutation(Java实现)

    这是悦乐书的第315次更新,第336篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第184题(顺位题号是784).给定一个字符串S,将每个字母单独转换为小写或大写以创建另 ...

  2. LeetCode算法题-Reshape the Matrix(Java实现)

    这是悦乐书的第264次更新,第277篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第131题(顺位题号是566).在MATLAB中,有一个非常有用的函数叫做'reshap ...

  3. LeetCode算法题-Implement Stack Using Queues

    这是悦乐书的第193次更新,第198篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第54题(顺位题号是225).使用队列实现栈的以下操作: push(x) - 将元素x推 ...

  4. LeetCode算法题-Subdomain Visit Count(Java实现)

    这是悦乐书的第320次更新,第341篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第189题(顺位题号是811).像"discuss.leetcode.com& ...

  5. LeetCode算法题-Jewels and Stones(Java实现)

    这是悦乐书的第313次更新,第334篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第182题(顺位题号是771).字符串J代表珠宝,S代表你拥有的石头.S中的每个字符都是 ...

  6. LeetCode算法题-Reach a Number(Java实现)

    这是悦乐书的第310次更新,第331篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第179题(顺位题号是754).你站在无限数字线的0号位置.在目的地有个target.在 ...

  7. LeetCode算法题-Shortest Completing Word(Java实现)

    这是悦乐书的第309次更新,第330篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第178题(顺位题号是748).从给定的字典单词中查找最小长度单词,其中包含字符串lic ...

  8. LeetCode算法题-Self Dividing Numbers(Java实现)

    这是悦乐书的第305次更新,第324篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第173题(顺位题号是728).自分割数是一个可被其包含的每个数字整除的数字.例如,12 ...

  9. LeetCode算法题-Find Pivot Index(Java实现)

    这是悦乐书的第304次更新,第323篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第172题(顺位题号是724).给定一个整数nums数组,编写一个返回此数组的" ...

随机推荐

  1. CSRF跨站伪造请求

    一.什么是CSRF CSRF(Cross Site Request Forgery) 跨站请求伪造.也被称为One Click Attack和Session Riding,通常缩写为CSRF或XSRF ...

  2. [转]比特币测试链——Testnet介绍

    本文转自:https://blog.csdn.net/wkb342814892/article/details/80796398 testnet使用详解需求需要搭建一个简单的交易测试场景,用于生成可查 ...

  3. 第五讲 smart qq poll包处理 以及 私聊 群聊消息收发

    发送 poll包 public static void Login_PostPoll() { try { string url = "http://d1.web2.qq.com/channe ...

  4. Java_Collections工具类

    Collections 工具类 * Collection与Collections区别 Collection 接口,(大部分集合类的实现接口) Collections 工具类(针对列表) * Colle ...

  5. 点到圆弧的距离(csu1503)+几何

    1503: 点到圆弧的距离 Time Limit: 1 Sec  Memory Limit: 128 MB  Special JudgeSubmit: 325  Solved: 70[Submit][ ...

  6. EF实现增删改查

    从来没想到过能在这个上面翻车,感慨自学没有培训来得系统啊,废话不多说 ORM:对象关系映射(Object Relational Mapping,简称ORM,或O/RM,或O/R mapping),是一 ...

  7. 带你使用JS-SDK自定义微信分享效果

    前言 想必各位在写wap端时都遇到过这样的场景吧 ----自定义分享标题.图片.描述 接下来小编给大家讲解下分享相关操作 预期效果 原始的分享效果: 使用微信JS-SDK的分享效果: 可以看出缩略图, ...

  8. 包含min函数的栈 ,二叉树的镜像

    包含min函数的栈 问题 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 代码 # -*- coding:utf-8 -*- class Sol ...

  9. 使用vmimeNET解析账单邮件

    大概所有做APP的公司都是不愿意做自定义的,哪怕自己的功能再烂也愿意慢慢修补不愿意开源一部分. 卡牛- 51信用卡- 一次次的逾期   自己写个信用卡管理工具,从邮件中提取账单,还款后做个登记,到了还 ...

  10. 14.Odoo产品分析 (二) – 商业板块(7) –制造(1)

    查看Odoo产品分析系列--目录 一旦你收到了库存中所需的原材料,就可以开始生产终端产品了.ERP系统的部分功能是帮助您根据可用资源调度这些订单.其中一项资源是原产品.其他资源可以包括可用劳动力或特定 ...