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 ...
随机推荐
- IP配置
1: #vi /etc/sysconfig/network-scripts/ifcfg-eth0 2: 实验环境-网络设置 公司域网: IP=162.168.16.0/24 netmask=255.2 ...
- 0329 复利计算器5.0 Juint单元测试 组员 254列志华 253韩麒麟
一.主要功能与需求分析 1.本金为100万,利率或者投资回报率为3%,投资年限为30年,那么,30年后所获得的利息收入:按复利计算公式来计算就是:1,000,000×(1+3%)^30 2.如果按照单 ...
- Promise机制
Promise的诞生与Javascript中异步编程息息相关,js中异步编程主要指的是setTimout/setInterval.DOM事件机制.ajax,通过传入回调函数实现控制反转.异步编程为js ...
- 基于MVC设计模式的两种软件架构简介
第一种模式,可处理组合命令,具有撤销(Undo)和重做(Redo)功能,支持多种数据库类型 1.Action采用组合模式,既可以代表一个简单的动作,也可以代表一组动作组合.List<Ac ...
- .NET Web开发总结
在aspx文件中 创建控件 在右下角有控件信息 按类排序 会将控件信息安装类排序 点击控件 会增加属性页面的分页[事件]页面 可以增加其事件函数 字符串操作及其时间操作 fn_name.Inser ...
- puppet实现主从部署各种软件实战参考模型
puppet实现主从部署各种软件实战参考模型 实验要求: 1.我将准备三个节点 node2 , node3 , node4 2.我们想让节点node3部署ntp,nginx ;节点node ...
- jquery easyui combobox
$("#select_Dic").combobox({ url: "http://www.cnblogs.com/Ajax/ ...
- phpMyAdmin安装
phpMyAdmin是MySql的一个Web操作界面. phpMyAdmin官网貌似被和谐了,经常无法访问.不过我们可以从GitHub下载phpMyAdmin. 然后解压,搭建与普通的PHP网站一样. ...
- Java迷题:等于,还是不等于?
等于还是不等于? 看来看下面的一段代码: public static void main(final String[] args) { Integer a = new Integer(100); In ...
- Modoer列表页性能分析及优化
在 http://www.modoer.org/beijing/item/list-8 的页面中,会执行以下2个sql SELECT s.sid,pid,catid,domain,name,avgso ...