【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/
题目: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).
/**
* 用两个栈模拟实现队列基本操作
* @author 徐剑
* @date 2016-02-25
*
*/
public class Solution
{
Stack<Integer> s1 = new Stack<>();
Stack<Integer> s2 = new Stack<>();
// Push element x to the back of queue.
public void push(int x)
{
s1.push(x); }
// Removes the element from in front of queue.
public void pop()
{
if (!s2.empty())
{
s2.pop();
} else if (s1.isEmpty())
{
return;
} else
{
while (!s1.isEmpty())
{
int temp = s1.pop();
s2.push(temp);
}
s2.pop();
}
}
// Get the front element.
public int peek()
{
if (!s2.isEmpty())
return s2.pop(); else if (s1.isEmpty())
{
return -1;
} else
{
while (!s1.isEmpty())
{
int temp = s1.pop();
s2.push(temp);
}
return s2.peek();
}
}
// Return whether the queue is empty.
public boolean empty()
{
return s1.isEmpty() && s2.isEmpty();
}
}
【LeetCode OJ 232】Implement Queue using Stacks的更多相关文章
- LeetCode(232) Implement Queue using Stacks
题目 Implement the following operations of a queue using stacks. push(x) – Push element x to the back ...
- 【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 232:Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back ...
- LeetCode 232: Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
- 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks
class Queue { public: // Push element x to the back of queue. void push(int x) { while (!nums.empty( ...
- 【LeetCode OJ 136】Single Number
题目链接:https://leetcode.com/problems/single-number/ 题目:Given an array of integers, every element appea ...
- 【LeetCode OJ 268】Missing Number
题目链接:https://leetcode.com/problems/missing-number/ 题目:Given an array containing n distinct numbers t ...
- 【LeetCode232】 Implement Queue using Stacks★
1.题目描述 2.思路 思路简单,这里用一个图来举例说明: 3.java代码 public class MyQueue { Stack<Integer> stack1=new Stack& ...
- 【LeetCode OJ 016】3Sum Closest
题目链接:https://leetcode.com/problems/3sum-closest/ 题目:Given an array S of n integers, find three integ ...
随机推荐
- BZOJ 2506 分块
//By SiriusRen #include <bits/stdc++.h> using namespace std; ; ][],g[N],tmp=; struct Node{int ...
- [Luogu 1052] noip 05 过河
[Luogu 1052] noip 05 过河 题目描述 在河上有一座独木桥,一只青蛙想沿着独木桥从河的一侧跳到另一侧.在桥上有一些石子,青蛙很讨厌踩在这些石子上.由于桥的长度和青蛙一次跳过的距离都是 ...
- ansible基础知识
安装ansible epel源 第一步: 下载epel源 wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel- ...
- mysql索引的操作
一.创建和查看普通索引 这是最基本的索引类型,而且它没有唯一性之类的限制 1.创建表时创建普通索引 CREATE TABLE table_name( 属性名 数据类型, ... 属性名 数据类型, I ...
- RabbitMQ~消息的产生和管理(15672)
上一讲说了rabbitmq在windows环境的部署,而今天主要说一下消息在产生后,如何去查看消息,事实上,rabbitmq为我们提供了功能强大的管理插件,我们只要开启这个插件即可,它也是一个网站,端 ...
- easyui textbox 内容改变事件 增加oninpu 类似事件,
//======================利用easyui验证功能,进行内容变化监控=== =============$(function () { var CustomerService = ...
- JS——祝愿墙
注意事项: 1.for循环的下一层注册了事件的话,事件函数中关于变量i的节点元素是不允许出现的,因为在函数加载的时候,只会加载函数名,不会加载函数体,外层for循环会走完一边,变量i一直会停留在最后一 ...
- html——快捷键
webstorm ctrl+c 复制 ctrl+v 粘贴 ctrl+x 剪切一行或删除一行 ctrl+d 复制本行到下一行 ctrl+z 退回之前操作 ctrl+shift+z 已经做好的退回之后的下 ...
- vim之<F12> 一键生成tags的一些小优化
在之前我写的<<vim之tags>>中最后提到将vim和tags成和更新的全部集中到一个<f12>键上来. 这在实践中证明是相当方便的, 不过依然村庄几个问题如下: ...
- sql的四种连接方式
1.内联接.(典型的联接运算,使用像 = 或 <> 之类的比较运算符).包括相等联接和自然联接. 内联接使用比较运算符根据每个表共有的列的值匹配两个表中的行.例如,检索 students ...