题目链接: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. linux命令(001) -- chkconfig

    一.准备知识 在说明chkconfig命令的用途之前,有必要先了解一下Linux系统中/etc/rc[0-6].d目录的用途. 众所周知,在Linux系统定义了7种不同的启动级别,这7种启动级别的含义 ...

  2. drupal 8——在CKEditor中导入video media时添加caption会导致video缩小至消失

    在CKEditor中,我点击media browser,选择video型的media,并在caption中输入video的名字.当我保存后发现在前台页面的video消失了,只留下video的名字,点击 ...

  3. CxImage实现9PNG

    CxImage* ScaleImageBy9PNG(CxImage *pRawImage, int nDstWidth,int nDstHeight) { if(NULL == pRawImage) ...

  4. R 连接数据库长数字被科学计数法解决方法

    数据库中的订单编号

  5. ASP.net参数传递总结

    同一页面.aspx与.aspx.cs之间参数传递 1. .aspx.cs接收.aspx的参数:由于.aspx和.aspx.cs为继承关系,所以.aspx.cs可以直接对.aspx中的ID进行值提取,具 ...

  6. [转]使用gdb调试异常

    有时程序中有未捕获的异常会导致程序异常的行为甚至导致程序的直接退出. 这对服务器程序来说是不可接受的. 可以使用gdb的catch命令来帮助我们调试异常. 使用gdb捕获异常的扔出点(相当于在扔出异常 ...

  7. Linux基础之网络协议

    互联网通信原理 从物理层面来说,每台计算机在一开始都是彼此孤立的,为了实现信息的交流与共享,计算机之间必须要建立通信网络.例如人与人之间的交流,他们必须要共用一套语言系统,才能交流成功.计算机之间也是 ...

  8. 四次挥手与tcp标志位

    鉴于tcp的标志位可以同时置位,在相应端无数据传输时,四次握手可以用三次报文完成.

  9. cocos creator destroy方法

    node.destroy(),Node.destroyAllChildren并不会立即销毁,实际销毁操作会延迟到当前帧渲染前执行. 这段话可能不明白,但是在Node.destroyAllChildre ...

  10. csrf漏洞利用

    low csrf(cross-site-request forgery),跨站请求伪造. 测试网站 --http://localhost/vulnerability/csrf 修改密码,点击chang ...