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 ...
随机推荐
- MyBatis学习系列二——增删改查
目录 MyBatis学习系列一之环境搭建 MyBatis学习系列二——增删改查 MyBatis学习系列三——结合Spring 数据库的经典操作:增删改查. 在这一章我们主要说明一下简单的查询和增删改, ...
- MAC OS下免费下载YouTube
YouTube上有很多不错的视频,你感兴趣的视频除了可以加入自己播放列表之外,还可以将其下载到本地收藏起来.推荐这款软件“Xilisoft Download YouTube Video for Mac ...
- Linux程序分析工具:ldd和nm
ldd和nm是Linux下两个非常实用的程序分析工具.其中,ldd是用来分析程序运行时需要依赖的动态链接库的工具,nm是用来查看指定程序中的符号表信息的工具. 1 ldd 格式:ldd [option ...
- SQL SERVER连接字符串学习
在使用connection string时遇到一些问题 字符串如下"Data Source= ******;Initial Catalog=******;Persist Security I ...
- mysql报错Table '.\erchina_news\v9_search' is marked as crashed and should be repaired
直切正题 报该问题的是表引导坏了,需要修复表就行 方法一: 找到mysql的安装目录的bin/myisamchk工具,在命令行中输入: myisamchk -c -r ../data/erchina_ ...
- UITableView去除空白cell上多余separator
具体的效果可以参考微信ios7版的UITableview 它最后一行cell的separator是顶到最左边的 首先设置tableFooterView _messageTableview.tableF ...
- 使用@media做自适应
@media (min-width: 768px){ //>=768的设备 } @media (max-width: 1199){ //<=1199的设备 }
- django-url调度器-初级篇
Django 遵从 MVC 模型,并将其特色化为 MTV 模型.模型的核心是通过用户访问的 url 来指向处理的函数,而函数处理后返回相应的结果.所以url决定了用户访问的入口,另外表单处理的提交地址 ...
- java clone简单学习
最近在帮忙写单侧的时候,经常会和这几个对象类打交道,因为对java也不是很熟悉,刚好学习一下,都是很浅的学习,并没有深入的去学习哈,因为感觉也用不上. protected Object clone() ...
- eval和new Function的区别
eval和new Function都可以动态解析和执行字符串.但是它们对解析内容的运行环境判定不同. var a = 'global scope' function b(){ var a = 'loc ...