LintCode Implement Queue by Two Stacks
1. stack(先进后出):
pop 拿出并返回最后值; peek 返回最后值; push 加入新值在后面并返回此值。
2. queue(先进先出) :
poll = remove 拿出并返第一个值; element = peek 返第一个值; add = offer 加入新值在后面并返回true/false。
做此题时, 第一个stack为基础, 第二个stack为媒介来颠倒顺序, 加时从第一个加, 取时从第二个取。
public class Queue {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public Queue() {
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
// do initialization if necessary
}
public void push(int element) {
stack1.push(element);
// write your code here
}
public int pop() {
if(!stack2.empty()){
return stack2.pop();
} else{
while(stack1.size() > 0){
stack2.push(stack1.pop());
}
if(!stack2.empty()){
return stack2.pop();
} else return -1;
}
// write your code here
}
public int top() {
if(!stack2.empty()){
return stack2.peek();
} else{
while(stack1.size() > 0){
stack2.push(stack1.pop());
}
if(!stack2.empty()){
return stack2.peek();
} else return -1;
}
// write your code here
}
}
LintCode Implement Queue by Two Stacks的更多相关文章
- Implement Queue by Two Stacks & Implement Stack using Queues
Implement Queue by Two Stacks Implement the following operations of a queue using stacks. push(x) -- ...
- Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks 原题链接 : http://lintcode.com/zh-cn/problem/implement-queue-by-stacks/# As th ...
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
3.5 Implement a MyQueue class which implements a queue using two stacks. LeetCode上的原题,请参见我之前的博客Imple ...
- 40. Implement Queue by Two Stacks【medium】
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- lintcode :implement queue by two stacks 用栈实现队列
题目 用栈实现队列 正如标题所述,你需要使用两个栈来实现队列的一些操作. 队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素. pop和t ...
- Implement Queue by Two Stacks
As the title described, you should only use two stacks to implement a queue's actions. The queue sho ...
- 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( ...
- lc面试准备:Implement Queue using Stacks
1 题目 Implement the following operations of a queue using stacks. push(x) -- Push element x to the ba ...
随机推荐
- https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
https://developers.google.com/maps/documentation/javascript/examples/places-autocomplete-addressform
- java day2一个模拟双色球的代码
package day2; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt ...
- 同上! 下拉复选框 点击当前的checkbox 选中后面li 添加到指定区域
(function() { $(".cxbtntj").click(function(){ console.log($("#jsLi1").attr(" ...
- mac brew mysql 启动之后报错
打开电脑 链接mysql 发现报错,连不上,应该是没自启动, 之前一直用windows电脑,就用mysql start 准备启动下,发现报错, p.p1 { margin: 0.0px 0.0px 0 ...
- Javascript DOM编程艺术 语法部分
1.变量,可以变化的东西我们称为变量,随着年龄的增大,我们的age不断变大 2.Javascript变量声明用var,可以不声明变量类型.尽量声明为一个字符串字面量. 3.弱类型:要求程序员必须明确的 ...
- 怎么利用javascript删除字符串中的最后一个字符呢?
程序员就是每天在各种代码下不停的调试,世界买家网最近遇到了烦心事,是什么事情呢? 需求是一个字符串,想删除这个字符串最后一个字符,比如“1,2,3,4,5,”,删除最后一个“,”用javascript ...
- IOS自定义日历控件的简单实现(附思想及过程)
因为程序要求要插入一个日历控件,该空间的要求是从当天开始及以后的六个月内的日历,上网查资料基本上都说只要获取两个条件(当月第一天周几和本月一共有多少天)就可以实现一个简单的日历,剩下的靠自己的简单逻辑 ...
- 论文 查重 知网 万方 paperpass
相信各个即将毕业的学生或在岗需要评职称.发论文的职场人士,论文检测都是必不可少的一道程序.面对市场上五花八门的检测软件,到底该如何选择?选择查重后到底该如何修改?现在就做一个知识的普及.其中对于中国的 ...
- Caliburn.Micro学习笔记目录——Zhouyongh
解析Caliburn.Micro(一) 解析Caliburn.Micro(二) 解析Caliburn.Micro(三) 解析Caliburn.Micro(四) Illusion = Caliburn. ...
- 编程等宽字体Source Code Pro(转)
Source Code Pro - 最佳的免费编程字体之一!来自 Adobe 公司的开源等宽字体下载 每一位程序员都有一套自己喜爱的代码编辑器与编程字体,譬如我们之前就推荐过一款"神 ...