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 ...
随机推荐
- PAT Basic 1029
1029 旧键盘 旧键盘上坏了几个键,于是在敲一段文字的时候,对应的字符就不会出现.现在给出应该输入的一段文字.以及实际被输入的文字,请你列出肯定坏掉的那些键. 输入格式: 输入在2行中分别给出应该输 ...
- Android自动化测试Uiautomator--UiCollection接口简介
这个对象可以理解为一个对象的集合,因为UiSelector描述后得到的有可能是多个满足条件的控件集合,因此可以用来生成UiCollection,继承自UiObject. 用于枚举一个容器的用户界面(U ...
- unittest的discover方法使用
使用unittest进行测试,如果是需要实现上百个测试用例,把它们全部写在一个test.py文件中,文件会越来越臃肿,后期维护页麻烦.此时可以将这些用例按照测试功能进行拆分,分散到不同的测试文件中. ...
- CF802D
D. Marmots (easy) time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- hdu6060[贪心+dfs] 2017多校3
/* hdu6060[贪心+dfs] 2017多校3*/ #include <bits/stdc++.h> using namespace std; typedef long long L ...
- 【Luogu】P3195玩具装箱(斜率优化DP)
这题还是比较炫的 题目链接 我们设f[i]是已经装了前i个玩具,且第i个玩具是某箱子里装的最后一个东西(废话) 那我们很轻松可以想到一个转移方程 ;i<=n;++i) ;j<i;++j) ...
- BZOJ 3205 [Apio2013]机器人 ——斯坦纳树
腊鸡题目,实在卡不过去. (改了一下午) 就是裸的斯坦纳树的题目,一方面合并子集,另一方面SPFA迭代求解. 优化了许多地方,甚至基数排序都写了. 还是T到死,不打算改了,就这样吧 #include ...
- 启动第一个 KVM 虚机
本节演示如何使用 virt-manager 启动 KVM 虚机. 首先通过命令 virt-manager 启动图形界面 1 # virt-manager 点上面的图标创建虚机 给虚机命名为 kvm1, ...
- 16.1113 模拟考试T1
笔记[问题描述]给定一个长度为m的序列a,下标编号为1~m.序列的每个元素都是1~N的整数.定义序列的代价为累加(1->m-1 abs(ai+1-ai))你现在可以选择两个数x和y,并将序列?中 ...
- Wiley出版 SQL Server 2005宝典
原文发布时间为:2008-07-30 -- 来源于本人的百度文章 [由搬家工具导入] Wiley出版 SQL Server 2005宝典 迅雷专用高速下载 thunder://QUFmdHA6L ...