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 ...
随机推荐
- Windows 2003 + IIS6.0 相关 401.1 或 401.2 等问题解决
我自己处理的过程: 1. 问题场景: 1.1 我本身有一个虚拟机为 win2003 + iis6.0 的环境 c:\ 为NTFS 格式 D:\为Fat32 格式 1.2 我需要部署一个 asp.net ...
- nginx + php +mysql (适配thinkphp)
Nginx 单机配置 http://tengine.taobao.org/book/index.html (taobao book) http://ubuntuhandbook.org/index.p ...
- MyEclipse使用手册(详细版)
0. 快捷键================================================================================编辑:Ctrl+Shift+ ...
- 安装 android sdk 不能更新问题
1 要更改host 文件 2在Android SDK Manager的Tool->Option中按照如下修改
- Ajax清除浏览器js、css、图片缓存的方法
做东东时都是把图片在服务器的地址存放在数据库里面,然后到浏览器中显示,但是后来发现了两个问题. 第一:为了安全起见,js是无法读取本地的图片的,不然你写一个js,岂不是可以获取任何人电脑里面的文件了. ...
- 转载Mongondb
转自(http://blog.csdn.net/lchjustc/article/details/16988251) Mongodb调研 1. 调研目的 现在公司缺乏一个通用的key-value存 ...
- c#使用DocX给word添加目录TOC
刚要做目录的时候,我就想DocX应该提供了这个方面的函数.于是我就在讨论区搜索,看看别人是怎么用的. 我搜索了catalog; catalogue; list; contents;,但是都没有和目录有 ...
- MongoDB(4):多种方式关闭服务命令
http://blog.csdn.net/czw698/article/details/8791153 MongoDB 提供几种关闭服务的命令,具体为以下: 一 使用 Crtl+C 关闭 [mong ...
- 自定义Toast的显示效果
Activity: package com.example.editortoast; import android.app.Activity; import android.os.Bundle; im ...
- Delphi 7 里没有加载的控件
在原来版本如D5.D6中使用的控件如Quickrep,FastNet等,在D7中仍然是保留的.只是Delphi没有将他们默认的安装到组件面版中来.这些控件包全部保存在Delphi目录的bin下,文件扩 ...