232. 用栈实现队列

232. Implement Queue using Stacks

题目描述

使用栈实现队列的下列操作:

  • push(x) -- 将一个元素放入队列的尾部。
  • pop() -- 从队列首部移除元素。
  • peek() -- 返回队列首部的元素。
  • empty() -- 返回队列是否为空。

每日一算法2019/5/7Day 4LeetCode232. Implement Queue using Stacks

示例:

MyQueue queue = new MyQueue();

queue.push(1);

queue.push(2);

queue.peek(); // 返回 1

queue.pop(); // 返回 1

queue.empty(); // 返回 false

说明:

  • 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。
  • 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。
  • 假设所有操作都是有效的(例如,一个空的队列不会调用 pop 或者 peek 操作)。

Java 实现

import java.util.Stack;
class MyQueue { Stack<Integer> input = new Stack();
Stack<Integer> output = new Stack(); public void push(int x) {
input.push(x);
} public void pop() {
peek();
output.pop();
} public int peek() {
if (output.empty())
while (!input.empty())
output.push(input.pop());
return output.peek();
} public boolean empty() {
return input.empty() && output.empty();
}
}

参考资料

LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4的更多相关文章

  1. LeetCode 232:用栈实现队列 Implement Queue using Stacks

    题目: 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从队列首部移除元素. peek() -- 返回队列首部的元素. empty() -- 返回队列是 ...

  2. [Swift]LeetCode232. 用栈实现队列 | Implement Queue using Stacks

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

  3. Java实现 LeetCode 232 用栈实现队列

    232. 用栈实现队列 使用栈实现队列的下列操作: push(x) – 将一个元素放入队列的尾部. pop() – 从队列首部移除元素. peek() – 返回队列首部的元素. empty() – 返 ...

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 记一次vue+vuex+vue-router+axios+elementUI开发(一)

    记录自己的vue开发之旅,方便之后查询 一.开发环境 1.安装node.js  自带npm https://nodejs.org/en/ 2. 全局安装vue-cli脚手架 npm install v ...

  2. Vue编程基础

    一.依赖环境搭建: 添加镜像 # 安装好node.js后,使用淘宝镜像 npm install -g cnpm --registry=https://registry.npm.taobao.org 项 ...

  3. 大数据技术之kettle(1)——安装

    一. kettle概述 1.kettle是一款开源的ETL工具,纯java编写,可以在Windows.Linux.Unix上运行,绿色无需安装,数据抽取高效稳定. 2.kettle的两种设计 简述: ...

  4. C# ZIP 压缩解压

    ZIP流是在NetFramework4.5 引入的目的是为了能够更好的操作ZIP文件,进行压缩解压等操作.与ZIP流相关的几个类是: ZipArchive 代表一个ZIP的压缩包文件 ZipArchi ...

  5. MATLAB中 H(b > g) = 2*pi - H(b > g); 作何解

    H(b > g) = 2*pi - H(b > g); %b > g 会得到一个逻辑矩阵,如b=[7,5,6] ;g=[1,2,8],那么b>g会得到[1,1,0]: b< ...

  6. python制作简单excel统计报表1之with的简单用法

    # coding=utf-8 def open_file(): """使用with打开一个文件""" # 普通操作文件方法 # f = op ...

  7. 生产环境zabbix3.2上亿的表数据通过表分区的方式进行历史数据清理

    生产环境zabbix3.2上亿的表数据通过表分区的方式进行历史数据清理 zabbix服务器经常报警io过载,在报警的时候发现是数据库在删除历史数据时耗时较长 数据库积攒了大量的历史数据信息,主要集中在 ...

  8. pytorch 不使用转置卷积来实现上采样

    上采样(upsampling)一般包括2种方式: Resize,如双线性插值直接缩放,类似于图像缩放,概念可见最邻近插值算法和双线性插值算法——图像缩放 Deconvolution,也叫Transpo ...

  9. 003-结构型-03-代理模式(Proxy)

    一.概述 Proxy模式又叫做代理模式,是构造型的设计模式之一,它可以为其他对象提供一种代理(Proxy)以控制对这个对象的访问. 可以详细控制访问某个类或对象的方法,在调用这个方法(流程代码放到代理 ...

  10. visual studio code 调试ROS的插件

    ctrl+p搜索: ext install ros https://marketplace.visualstudio.com/items?itemName=ajshort.ros 进行安装 其他可以调 ...