LeetCode——Implement Queue using Stacks
Description:
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 top
,size
, andis 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).
用栈来实现队列的功能。类似的还有用队列实现栈的功能。
思路:栈和队列对数据的处理是不同的。栈是先进后出(FILO)队列是先进先出(FIFO)。所以要用两个栈来维护一个队列。一个数据栈,一个暂存数据栈。数据栈用来存储数据,暂存数据栈用来把数据栈中的数据首尾交换,模拟队列的数据操作。
代码:
class MyQueue {
Stack<Integer> stack1;
Stack<Integer> stack2; public MyQueue() {
stack1 = new Stack();
stack2 = new Stack();
} // Push element x to the back of queue.
public void push(int x) {
stack1.push(x);
} // Removes the element from in front of queue.
public void pop() {
//把栈中的元素移到另一个栈中,首尾倒序。
while(!stack1.empty()) {
stack2.push(stack1.pop());
}
//移除堆首元素。
stack2.pop();
//还原数据队列。
while(!stack2.empty()) {
stack1.push(stack2.pop());
}
} // Get the front element.
public int peek() {
//把栈中的元素移到另一个栈中,首尾倒序。
while(!stack1.empty()) {
stack2.push(stack1.pop());
}
//获取堆首元素。
int front = stack2.peek();
//还原数据队列。
while(!stack2.empty()) {
stack1.push(stack2.pop());
}
return front;
} // Return whether the queue is empty.
public boolean empty() {
return stack1.empty();
}
}
LeetCode——Implement Queue using Stacks的更多相关文章
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- Leetcode Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- LeetCode Implement Queue using Stacks (数据结构)
题意: 用栈来实现队列. 思路: 一个栈是不够的,至少要两个. (1)插入.永远只插入到stack1中(插到栈顶). (2)弹出.如果stack2不为空,直接弹出stack2的栈顶,否则,将stack ...
- LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- leetcode:Implement Stack using Queues 与 Implement Queue using Stacks
一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...
- 【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( ...
- 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 ...
- Leetcode 232 Implement Queue using Stacks 和 231 Power of Two
1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- FusionCharts JavaScript API - Functions 常用方法整理笔记
FusionCharts JavaScript API - Functions Home > FusionCharts XT and JavaScript > API Reference ...
- HTML资料——做网页时遇到的
HTML <meta> 标签 浏览器支持 IE Firefox Chrome Safari Opera 所有浏览器都支持 <meta> 标签. 定义和用法 ...
- win10计算机打开不要是“快速访问”功能?
win10的文件夹选项中---常规--最上端调整
- Hibernate- 基本查询
01.搭建开发环境 02.基本查询 package com.gordon.test; import java.text.DecimalFormat; import java.util.Arrays; ...
- java-动态获取项目根路径
${ pageContext.request.contextPath } <hr> <a href="${ pageContext.request.contextPath ...
- @RequestMapping @ResponseBody 和 @RequestBody 注解的用法与区别
1.@RequestMapping 国际惯例先介绍什么是@RequestMapping,@RequestMapping 是一个用来处理请求地址映射的注解,可用于类或方法上.用于类上,表示类中的所有响应 ...
- C语言中带参数的宏
带参数的宏定义有如下的格式: [#define 指令----带参数的宏] #define 标识符(x1,x2,……,xn) 其中 x1,x2,……xn是标志符(宏的参数) 注意:在宏的名字和括号之间 ...
- AOP——引言
转自:http://wayfarer.cnblogs.com/articles/241012.html 1.引言 2.AOP技术基础3.Java平台AOP技术研究 4..Net平台AOP技术研究 软件 ...
- Anaconda之常用命令
1.查看环境列表:conda-env list 2.删除环境:conda env remove -n tf1.2 3.创建指定python的环境:conda create -n tf1.2 pyth ...
- linux -- ubuntu修改IP地址、网关、dns
ubuntu系统 一.使用命令设置Ubuntu IP地址 1.修改配置文件blacklist.conf禁用IPV6 sudo vi /etc/modprobe.d/blacklist.conf 表示用 ...