LintCode-Implement Queue by Stacks
As the title described, you should only use two stacks to implement a queue's actions.
The queue should support push(element), pop() and top() where pop is pop the first(a.k.a front) element in the queue.
Both pop and top methods should return the value of first element.
For push(1), pop(), push(2), push(3), top(), pop(), you should return 1, 2 and 2
implement it by two stacks, do not use any other data structure and push, pop and top should be O(1) by AVERAGE.
Solution:
public class Solution {
private Stack<Integer> stack1;
private Stack<Integer> stack2; public Solution() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
} public void push(int element) {
stack2.push(element);
} private void shuffle(){
while (!stack2.isEmpty())
stack1.push(stack2.pop());
} public int pop() {
if (stack1.isEmpty()) shuffle();
return stack1.pop();
} public int top() {
if (stack1.isEmpty()) shuffle();
return stack1.peek();
}
}
LintCode-Implement Queue by Stacks的更多相关文章
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- 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) 4
232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...
- [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 ...
- Implement Queue using Stacks
Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...
随机推荐
- iOS 8.3 JB ready
Hi, I've been waiting for a very very long time..Now iOS 8.3 is ready. http://www.taig.com/ You guys ...
- 基本的Web控件一
ASP.NET提供了与HTML元素相对应的基本Web控件,ASP.NET提供的基本的Web控件如下: 基本的Web控件 对应的HTML元素 Label ----------------- ...
- Android数据库(sqlite)加密方案
最近因为一些项目的安全性需要将数据库加密,一开始想到的就是先将数据库通过AES加密,然后运行时再解密,另一种是将数据库里的内容加密. 很快这两种方案都是不理想的,第一种加密方式形同虚设,第二种,如果加 ...
- php的命名规范
1.类 类名每一个单词首字母大写,如类名StudentCourse. 2.常量 常量名所有字母大写,单词间用下划线分隔,如常量名NULL.TRUE.FALSE.ROOT_PATH等. 3.变量 为了保 ...
- MySQL的存储引擎整理
01.MyISAM MySQL 5.0 以前的默认存储引擎.MyISAM 不支持事务.也不支持外键,其优势是访问的速度快,对事务完整性没有要求或者以SELECT.INSERT 为主的应用基本上都可以使 ...
- 移植u-boot.2012.04.01
/*************************************************** *u-boot版本:u-boot2012.04.01 *gcc版本:arm-linux-gcc ...
- 导出excel表功能
前台: <asp:Button ID="btndao" runat="server" Text="导出excel文件" onclic ...
- ThinkPHP之中的验证码的小示例
ThinkPHP之中已经封装好了验证码的调用,但是关于手册,缺失了HTML之中以及.实际操作之中的点击ajax就会刷新验证码ajax代码:现在分享一下:看客老爷们注意啦! 放大招啦!!!三分归元气-- ...
- 对话框AlertDialog的基本类型与创建
测试代码: 布局: activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...
- delphi 连接MYSQL 的方法
需要的控件:(view as form) 1.SQLConnection1: TSQLConnection ConnectionName = 'MYSQLCONNECTION' DriverName ...