Lintcode: Implement Queue by Stacks 解题报告
Implement Queue by Stacks
原题链接 : http://lintcode.com/zh-cn/problem/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 1:
使用两个栈,stack1和stack2。
http://www.ninechapter.com/problem/49/
public class Solution {
private Stack<Integer> stack1;
private Stack<Integer> stack2;
public Solution() {
// do initialization if necessary
stack1 = new Stack<Integer>();
stack2 = new Stack<Integer>();
}
public void push(int element) {
// write your code here
stack1.push(element);
}
public int pop() {
// write your code here
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.pop();
}
public int top() {
// write your code here
// write your code here
if (stack2.isEmpty()) {
while (!stack1.isEmpty()) {
stack2.push(stack1.pop());
}
}
return stack2.peek();
}
}
GITHUB:
https://github.com/yuzhangcmu/LeetCode_algorithm/blob/master/lintcode/stack/StackQueue.java
Lintcode: Implement Queue by Stacks 解题报告的更多相关文章
- 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...
- 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 ...
- Java [Leetcode 232]Implement Queue using Stacks
题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...
随机推荐
- 过滤IP地址的正则表达式
现场需求,过滤 指定IP段位的相关话单,收集看看用正则表达式怎么写, 原文地址:http://www.cnblogs.com/kongxianghai/p/3995463.html 检测IP地址的正则 ...
- cd及目录快速切换
一.cd ~ 切换到用户目录 二.cd - cd - 返回进入当前目录前所在目录 三.pushd.popd.dirs 在Linux的多目录命令提示符中工作是一种痛苦的事情,但以下这些利用lin ...
- 【Spring】Spring+struts2+Hibernate框架的搭建
1.搭建过程 首先需要引入Spring.Struts2.Hibernate的开发包,已经数据库的驱动包. UserAction.java文件 package cn.shop.action; impor ...
- Mac Apache Tomcat 配置
1.配置准备工作 1)配置 Tomcat 准备工作 下载相关软件 apache-tomcat-9.0.6.zip tomcat 官网 Tomcat 配置软件下载地址,密码:sgrn. 2)配置注意事项 ...
- List元素排序简例
前言:这种处理方式,在程序中偶尔会用的到,栗子很简单,关键是加强一下记忆,以及以备后用 1:实现Comparable接口的方式 1-1:没有使用泛型,重写compareTo()方法时需要判断类型及转换 ...
- Oracle 12C -- 在相同的列的集合上创建多个索引
在12C中,可以在相同的列的集合上创建多个索引,但是多个索引的类型要不同.同一时刻,只有一个是可见的. SQL> create table emp_tab as select * from em ...
- MySQL经常使用技巧
建表 每一个表都有一个id字段,最好为UNSIGNED.如 INT(9) UNSIGNED NOT NULL 在表使用一段时间后,使用PROCEDURE ANALYSE得到建议,如select * f ...
- redis cluster 集群重新启动关闭
找遍了redis cluster官方文档,没发现有关集群重新启动和关闭的方法.为啥会没有呢,推測redis cluster至少要三个节点才干执行,三台同一时候挂掉的可能性比較小,仅仅要不同一时候挂掉. ...
- SQL 中的 UNION 和UNION ALL 的区别
UNION表示“并”,当用的时候,系统会自动将重复的元组去掉,如果要保留重复元组则就用UNION ALL UNION 会合并重复数据,(由于要合并重复,该操所 隐藏着一个 排序的操作.)UNION A ...
- win7 安装mysql 5.7.9记录
-------------------------------------------------------------------------- 1. 将配置文件my.ini配置好,放到c:/wi ...
