题目链接:https://leetcode.com/problems/implement-queue-using-stacks/

题目:Implement the following operations of a queue using stacks.

  • push(x) -- Push element x to the back of queue.
  • pop() -- Removes the element from in front of queue.
  • peek() -- Get the front element.
  • empty() -- Return whether the queue is empty.

Notes:

  • You must use only standard operations of a stack -- which means only push
    to top
    peek/pop from topsize,
    and is empty operations are valid.
  • Depending on your language, stack may not be supported natively. You may simulate a stack by using a list or deque (double-ended queue), as long as you use only standard operations of a stack.
  • You may assume that all operations are valid (for example, no pop or peek operations will be called on an empty queue).
解题思路:题目要求用栈来实现队列,并实现其入队列、出队列、查看队首元素和推断队列是否为空四个操作。大致的思路是:用两个栈来模拟实现队列,栈s1作为存储空间,栈s2作为暂时缓冲区。
入队时:将元素压入s1
出队时:首先推断s2是否为空,如不为空,则直接弹出栈顶元素,假设为空,则将s1的元素逐个"倒入"s2,把最后一个元素弹出并出队。

演示样例代码例如以下:
/**
* 用两个栈模拟实现队列基本操作
* @author 徐剑
* @date 2016-02-25
*
*/
public class Solution
{
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
// Push element x to the back of queue.
public void push(int x)
{
s1.push(x); }
// Removes the element from in front of queue.
public void pop()
{
if (!s2.empty())
{
s2.pop();
} else if (s1.isEmpty())
{
return;
} else
{
while (!s1.isEmpty())
{
int temp = s1.pop();
s2.push(temp);
}
s2.pop();
}
}
// Get the front element.
public int peek()
{
if (!s2.isEmpty())
return s2.pop(); else if (s1.isEmpty())
{
return -1;
} else
{
while (!s1.isEmpty())
{
int temp = s1.pop();
s2.push(temp);
}
return s2.peek();
}
}
// Return whether the queue is empty.
public boolean empty()
{
return s1.isEmpty() && s2.isEmpty();
}
}

【LeetCode OJ 232】Implement Queue using Stacks的更多相关文章

  1. LeetCode(232) Implement Queue using Stacks

    题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...

  2. 【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( ...

  3. LeetCode 232:Implement Queue using Stacks

     Implement the following operations of a queue using stacks. push(x) -- Push element x to the back ...

  4. LeetCode 232: Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  5. 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks

    class Queue { public: // Push element x to the back of queue. void push(int x) { while (!nums.empty( ...

  6. 【LeetCode OJ 136】Single Number

    题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...

  7. 【LeetCode OJ 268】Missing Number

    题目链接:https://leetcode.com/problems/missing-number/ 题目:Given an array containing n distinct numbers t ...

  8. 【LeetCode232】 Implement Queue using Stacks★

    1.题目描述 2.思路 思路简单,这里用一个图来举例说明: 3.java代码 public class MyQueue { Stack<Integer> stack1=new Stack& ...

  9. 【LeetCode OJ 016】3Sum Closest

    题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...

随机推荐

  1. 为什么使用HttpServlet?http协议特点、servlet

    因为只有HttpServlet是基于http协议,实现Servlet接口,而http协议是短连接协议,能够实现客户端访问服务端后,数据交互后 连接自动断开.同时http协议基于tcp.ip协议,封装了 ...

  2. 高斯消元_HihoCoderOffer6_03

    题目3 : 图像算子 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 在图像处理的技术中,经常会用到算子与图像进行卷积运算,从而达到平滑图像或是查找边界的效果. 假设原图 ...

  3. [ CQOI 2009 ] 中位数图

    \(\\\) \(Description\) 给出\(N\)的一个全排列,统计该排列有多少个长度为奇数的连续子序列,中位数是\(B\). \(N\in [0,10^5]\),\(B\in [0,N]\ ...

  4. sql server 数据分析优化实战(一)——SQL语句优化

    前言 在我们进行数据分析的时候,首要的目标是根据业务逻辑,通过编写SQL代码得到我们想要的结果,这是毋庸置疑的.一般情况下,由于我们分析的数据量比较少,体会不出SQL语句各种写法的性能优劣,对SQL代 ...

  5. Linux监控实时log

    https://jingyan.baidu.com/article/93f9803f5545a3e0e46f5596.html

  6. 正文处理命令及tar命令

    使用cat命令进行文件的纵向合并,具体命令如下所示(注意:>代表将左边命令的执行结果以覆盖的方式放到右边,>>代表将左边命令的执行结果追加到右边) 关于tar命令的一些用法: tar ...

  7. servlet学习总结(一)——HttpServletRequest(转载)

    原文地址:http://www.cnblogs.com/xdp-gacl/p/3798347.html 一.HttpServletRequest介绍 HttpServletRequest对象代表客户端 ...

  8. 移动的 touch事件中的touches、targetTouches和changedTouches

    touches: 当前屏幕上所有触摸点的列表; targetTouches: 当前对象上所有触摸点的列表; changedTouches: 涉及当前(引发)事件的触摸点的列表 通过一个例子来区分一下触 ...

  9. node里读取命令行参数

    一.process.env process.env属性返回一个包含用户环境信息的对象. 最常见的需求,前端需要根据不同的环境(dev,prd),来调用不同的后端接口.如果用webpack,是这么做的: ...

  10. js 简单小知识

    1. javascript的typeof返回哪些数据类型: string, boolean, number, undefined, function, object 2. split() join() ...