题目:

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 emptyoperations 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).

解题:

用栈实现队列,这个比用队列实现栈要麻烦一些,这里用到两个栈,两种思路。第一种就是在进栈的时候,把栈逆序放在另外一个栈,出的时候直接出另外一个栈就能够了。另外一种思路。进栈的时候不做不论什么处理。出栈的时候把栈逆序放在另外一个栈。出另外一个栈。以下就是两种的代码实现。

在进栈的时候进行处理:

class MyQueue {
// Push element x to the back of queue.
Stack<Integer> stack=new Stack<>();
Stack<Integer> stack2=new Stack<>(); public void push(int x) {
while(!stack.isEmpty())
{
stack2.push(stack.pop());
}
stack2.push(x);
while(!stack2.isEmpty())
{
stack.push(stack2.pop());
} } // Removes the element from in front of queue.
public void pop() {
stack.pop();
} // Get the front element.
public int peek() {
return stack.peek();
} // Return whether the queue is empty.
public boolean empty() {
return stack.isEmpty();
}
}

在出栈的时候进行处理:

class MyQueue2 {
// Push element x to the back of queue.
Stack<Integer> stack=new Stack<>();
Stack<Integer> stack2=new Stack<>(); public void push(int x) {
while(!stack2.isEmpty())
stack.push(stack2.pop());
stack.push(x); } // Removes the element from in front of queue.
public void pop() { while(!stack.isEmpty())
stack2.push(stack.pop());
stack2.pop(); } // Get the front element.
public int peek() {
while(!stack.isEmpty())
stack2.push(stack.pop());
return stack2.peek(); } // Return whether the queue is empty.
public boolean empty() {
while(!stack2.isEmpty())
stack.push(stack2.pop());
return stack.isEmpty();
}
}

LeetCode232 Implement Queue using Stacks Java 题解的更多相关文章

  1. LeetCode232:Implement Queue using Stacks

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

  2. Lintcode: Implement Queue by Stacks 解题报告

    Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...

  3. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

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

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

  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. 232. Implement Queue using Stacks,225. Implement Stack using Queues

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

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

  8. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  9. 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...

随机推荐

  1. Robotium测试没有源码的apk--需重签名apk

    Robotium是基于Instrumentation框架的,其编写的测试脚本与被测程序运行在同一个进程里面,所以这需要测试程序与被测程序拥有相同的签名,否则无法进行通讯.在只有apk的情况下可以采用“ ...

  2. skkyk:题解 洛谷P3865 【【模板】ST表】

    我不会ST表 智推推到这个题 发现标签中居然有线段树..? 于是贸然来了一发线段树 众所周知,线段树的查询是log(n)的 题目中"请注意最大数据时限只有0.8s,数据强度不低,请务必保证你 ...

  3. Django的中间件及WSGI

    什么是中间件? 官方的说法:中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出.每个中间件组件都负责做一些特定的 ...

  4. IPy模块--IP地址处理

    Python之实用的IP地址处理模块IPy 实用的IP地址处理模块IPy 在IP地址规划中,涉及到计算大量的IP地址,包括网段.网络掩码.广播地址.子网数.IP类型等 别担心,Ipy模块拯救你.Ipy ...

  5. appium之android_uiautomator定位

    前言 appium就是封装android的uiautomator这个框架来的,所以uiautomator的一些定位方法也可以用 text 1.通过text文本定位语法 new UiSelector() ...

  6. 如何修改 WordPress 的默认 Gravatar 头像

    如何修改 WordPress 的默认 Gravatar 头像? wordpress默认的头像是下面这种 在Settings的Discussion中,默认选择第一个Mystery Person, 意思是 ...

  7. python3--命名空间字典

    命名空间字典 我们学到了模块的命名空间实际上是以字典的形式实现的,并且可以由内置属性__dict__显示这一点.类和实例对象也是如此:属性点号运算其实内部就是字典的索引运算,而属性继承其实就是搜索连结 ...

  8. [Kubernetes]Pod字段自动填充

    PodPreset(Pod预设置)在Kubernetes v1.11以后出现,开发人员只需要提交一个基本的Pod YAML,Kubernetes就可以自动给对应的Pod对象加上运维人员设定好的其他必要 ...

  9. [BZOJ1575] [Usaco2009 Jan]气象牛Baric(DP)

    传送门 DP f[i][j]表示前i个中选j个的最优解 预处理g[i][j]表示选i~j对答案的贡献 那么就可以n^3乱搞了! 注意边界 #include <cstdio> #includ ...

  10. 刷题总结——愤怒的小鸟(NOIPDAY2T3)

    题目: 题目背景 NOIP2016 提高组 Day2 T3 题目描述 Kiana 最近沉迷于一款神奇的游戏无法自拔.简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于 (0,0) 处,每次 Ki ...