Implement Queue by Two 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.
分析
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
public class Queue { private Stack<Integer> stack1 = new Stack<Integer>(); private Stack<Integer> stack2 = new Stack<Integer>(); public Queue() { // do initialization if necessary } public void push(int element) { // write your code here while(!stack1.empty()){ stack2.push(stack1.pop()); } stack1.push(element); while(!stack2.empty()){ stack1.push(stack2.pop()); } } public int pop() { // write your code here if(!stack1.empty()) return stack1.pop(); else return -1; } public int top() { // write your code here if(!stack1.empty()) return stack1.peek(); else return -1; }} |
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) -- ...
- [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
1. stack(先进后出): pop 拿出并返回最后值: peek 返回最后值: push 加入新值在后面并返回此值. 2. queue(先进先出) : poll = remove 拿出并返第一个值 ...
- lintcode :implement queue by two stacks 用栈实现队列
题目 用栈实现队列 正如标题所述,你需要使用两个栈来实现队列的一些操作. 队列应支持push(element),pop() 和 top(),其中pop是弹出队列中的第一个(最前面的)元素. pop和t ...
- 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 ...
- 232. Implement Queue using Stacks,225. Implement Stack using Queues
232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...
随机推荐
- 【转】CentOS Linux解决Device eth0 does not seem to be present(linux)
原文来自:http://www.linuxidc.com/Linux/2012-12/76248.htm 在VMware里克隆出来的CentOS Linux.. ifconfig...没有看到eth0 ...
- C#之Ref,Out以及TryParse()的用法
ref和out用法很简单,我记下来也提醒自己要用更好的方式去写代码,不要让代码过于臃肿,让人看得头痛.直接写 ref:ref关键字就是让参数进行传递,但是需要初始化,而out不需要初始化参数 ref用 ...
- JUC——原子类操作(三)
原子类操作 既然强调了并发访问,那么就必须考虑操作系统位数:32位操作系统还是64位操作系统,对于long型数据类型而言,是64位的.但是如果现在项目运行在32位系统上,则long型数据会占用32位空 ...
- NES像素风格的Raspberry
周末小实践,vue+树莓派+一言API 一直有个想法,让树莓派做后端,实现一个有趣的网络服务.可是,苦于不会前端,迟迟无法动手.最近由于工作任务需要研究了一下前端. 问过前端大佬们,个个都说你得用vu ...
- python程序设计——面向对象程序设计:继承
继承是为代码复用和设计复用而设计的 在继承关系中,已有的.设计好的类称为父类或基类,新设计的类为子类或派生类 派生类可以继承父类的公有成员,但不能继承其私有成员 如果需要在派生类中调用基类的方法,可以 ...
- leetcode个人题解——#40 Combination Sum2
思路:解法和39题类似,改动了两处: 1.因为题目要求每个元素只能出现一次(不代表每个数只能有一个,因为数据中会有重复的数字),所以代码中21行搜索时下一次循环的位置+1: 2.将临时存放答案的vec ...
- AutoCAD 自动管理字体插件[使用ObjectARX C++]
概述: 使用AutoCAD的过程中,我们常常因为缺失字体而烦恼,本插件就是为了解决这个问题. 插件采用WEB服务器 + CAD插件方式.WEB服务器使用Python编写,部署在百度BAE上:CAD插件 ...
- 常用的os操作方法
os.sep可以取代操作系统特定的路径分隔符.windows下为 “”os.name字符串指示你正在使用的平台.比如对于Windows,它是'nt',而对于Linux/Unix用户,它是'posix' ...
- 对其中的一个特点将NABC的分析结果
一.题目要求 每一个组员针对其中的一个特点将NABC的分析结果发表博客上(截止日期4月8日晚24:00前). 二.分析结果 特点之一:通讯方便 <渴了么>这个安卓APP特点之一就是通讯方便 ...
- Python 变量和常量及数据类型
一.变量的命名 变量由字母.数字和下划线组成.变量的第1个字符必须是字母或下划线. 二.变量的赋值 例: x = 1 三.局部变量 局部变量只能在函数或者代码段内使用. 四.全局变量 在函数之外定义的 ...