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. GoCN每日新闻(2019-09-24)

    1. Go 搭建的高效网页爬虫:https://creekorful.me/building-fast-modern-web-crawler/ 2. Go 时区处理:https://medium.co ...

  2. jmeter分布式压测原理简介1

    1.什么叫分布式压测? 分布式压测:模拟多台机器向目标机器产生压力,模拟几万用户并发访问 2.分布式压测原理:如下 3.更多补充.....待添加

  3. 编译安装-httpd-2.2.15.tar.gz

    编译安装(又称源代码安装) 找到httpd-2.2.15.tar.gz安装包并拖到桌面root文件夹里 解包阶段 tar zxf httpd-2.2.15.tar.gz -C /usr/src 配置阶 ...

  4. Linux压缩和解压类指令

    一.gzip / gunzip  指令 gzip 用于压缩文件,gunzip 用于解压文件. 基本语法gizp  文件 (功能描述:只能将文件压缩为*.gz文件)gunzip  文件.gz (功能描述 ...

  5. SDN上机第五次作业

    2019 SDN上机第五次作业 1.浏览RYU官网学习RYU控制器的安装和RYU开发入门教程,提交你对于教程代码的理解,包括但不限于: 1.1描述官方教程实现了一个什么样的交换机功能? 答:官方教程实 ...

  6. Rancher2.3.2部署Kubenetes Dashboard

    首先进入到集群中的System命令空间,因为kubenetes dashboard是给整个集群使用的,并不是默认的Default命名空间使用的 Default命名空间,是默认的命名空间,也是在部署其他 ...

  7. 蓝牙BLE: GATT Profile 简介(GATT 与 GAP)

    一. 引言 现在低功耗蓝牙(BLE)连接都是建立在 GATT (Generic Attribute Profile) 协议之上.GATT 是一个在蓝牙连接之上的发送和接收很短的数据段的通用规范,这些很 ...

  8. layui select 下拉框 级联 动态赋值 与获取选中值

    //下拉框必须在 class="layui-form" 里 不然监听事件没有作用 <div class="layui-form" > <div ...

  9. flutter SnackBar 底部消息提示

    具有可选操作的轻量级消息提示,在屏幕的底部显示 文档:https://api.flutter.dev/flutter/material/SnackBar-class.html demo: import ...

  10. java 读取CSV数据并写入txt文本

    java 读取CSV数据并写入txt文本 package com.vfsd; import java.io.BufferedWriter; import java.io.File; import ja ...