LeetCode232 Implement Queue using Stacks Java 题解
题目:
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 toppeek/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).
解题:
用栈实现队列,这个比用队列实现栈要麻烦一些,这里用到两个栈,两种思路。第一种就是在进栈的时候,把栈逆序放在另外一个栈,出的时候直接出另外一个栈就能够了。另外一种思路。进栈的时候不做不论什么处理。出栈的时候把栈逆序放在另外一个栈。出另外一个栈。以下就是两种的代码实现。
在进栈的时候进行处理:
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 题解的更多相关文章
- LeetCode232:Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) – Push element x to the back of ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- 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( ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
- 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(). 移除队首元 ...
- 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...
随机推荐
- apache2虚拟主机实现一个服务器绑定多个域名
1.apache2的配置 首先要配置好apache2,如果未配置,请参考我之前的博文:lamp的配置 2.域名的解析 将全部域名的www和@的A记录解析到云服务器的IP 3.虚拟主机的配置 1.配置h ...
- Python --写excel
# -*- coding: UTF-8 -*- import xlwt import StringIO # 将数据保存成excel def write_data(data, tname): file ...
- CSU-1336: Interesting Calculator,最短路思想!
1336: Interesting Calculator 这道题被LZQ抢了一血,于是去读题发现题意不难,纯广搜结果写的一塌糊涂. 题意:给你两个数x,y.由x每次可以经过一系列操作变换,每个变换都有 ...
- 【Luogu】P1972HH的项链(链表+树状数组)
题目链接 难题,所以会讲得细一些. 首先我们想如何统计区间[l,r]内不同贝壳的个数. 第一个思路就是线段树/树状数组,query(1,r)-query(1,l-1)对不对? 然而这样是不对的. 然后 ...
- Opencv学习笔记——视频高斯模糊并分别输出
用两个窗口进行对比 #include "stdafx.h" #include <iostream> #include <opencv2/core/core.hpp ...
- 通过Idea进行Kubernetes YAML开发
即将推出的IntelliJ IDEA 2018.1 Ultimate Edition通过全新的Kubernetes插件为Kubernetes引入了初步支持.新插件支持从v1.5到最近发布的v1.9 的 ...
- C/C++怎样传递二维数组,转载自CSDN
用二维数组作为参数传递(用二维数组处理矩阵),但是希望接受传递二维数组参数的函数可以处理任意维度的数组(希望矩阵的行数和列数都是不固定的). [以下转帖] ---------------------- ...
- 关于MySQL的事务处理及隔离级别
原文地址 :http://blog.sina.com.cn/s/blog_4c197d420101awhc.html 事务是DBMS得执行单位.它由有限得数据库操作序列组成得.但不是任意得数据库操作序 ...
- js动态适配移动端font-size,单位:rem
比如:目前我手中有一张psd图,大小为640*1010,适配苹果5的手机. 方法步骤: 1.我采用font-size=10px为640*1010手机的初始像素大小: 1rem=10px: 此时psd ...
- Fast I/O 模板
[来源:2017 Multi-University Training Contest - Team 1] //面包有毒:P #define BUF_SIZE 100000 //fread -> ...