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/

对于Queue的操作对应如下:
Queue.Push:
    push到Stack1
 
Queue.Pop:
    如果Stack2非空,Stack2.pop
    否则将Stack1中的所有数pop到Stack2中(相当于顺序颠倒了放入),然后Stack2.pop()
 
每个数进出Stack1和Stack2各1次,所以两个操作的均摊复杂度均为O(1)
 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 解题报告的更多相关文章

  1. 【LeetCode】232. Implement Queue using Stacks 解题报告(Python & Java)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 Python解法 Java解法 日期 [LeetCo ...

  2. leetcode:Implement Stack using Queues 与 Implement Queue using Stacks

    一.Implement Stack using Queues Implement the following operations of a stack using queues. push(x) - ...

  3. 【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( ...

  4. 232. Implement Queue using Stacks,225. Implement Stack using Queues

    232. Implement Queue using Stacks Total Accepted: 27024 Total Submissions: 79793 Difficulty: Easy Im ...

  5. 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 ...

  6. Leetcode 232 Implement Queue using Stacks 和 231 Power of Two

    1. 232 Implement Queue using Stacks 1.1 问题描写叙述 使用栈模拟实现队列.模拟实现例如以下操作: push(x). 将元素x放入队尾. pop(). 移除队首元 ...

  7. LeetCode 232. 用栈实现队列(Implement Queue using Stacks) 4

    232. 用栈实现队列 232. Implement Queue using Stacks 题目描述 使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部. pop() -- 从 ...

  8. Leetcode Implement Queue using Stacks

    Implement the following operations of a queue using stacks. push(x) -- Push element x to the back of ...

  9. Java [Leetcode 232]Implement Queue using Stacks

    题目描述: Implement the following operations of a queue using stacks. push(x) -- Push element x to the b ...

随机推荐

  1. HDU 1907 John (Nim博弈)

    John Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submis ...

  2. Android启动过程深入解析

    本文由 伯乐在线 - 云海之巅 翻译.未经许可,禁止转载!英文出处:kpbird.欢迎加入翻译小组. 当按下Android设备电源键时究竟发生了什么? Android的启动过程是怎么样的? 什么是Li ...

  3. mysql合并binlog

    例如: PURGE MASTER LOGS BEFORE DATE_SUB( NOW( ), INTERVAL DAY);

  4. Unable to convert MySQL date/time value to System.DateTime

    当使用.NET connector 连接MYSQL数据库,检索某些数据的时候,你可能会得到一个错误信息: "Unable to convert MySQL date/time value t ...

  5. python -- 装饰器入门

    用例: 统计函数执行需要的时间 假设我们执行的一段代码的运行时间比我们预想的时间要久,而这段代码块有多个函数调用组成,我们有理由相信至少是其中的一个函数调用导致整个代码块产生了瓶颈.我们如何去发现导致 ...

  6. IAR for stm8 memory窗口的功能

    进入debug模式后点击菜单view-Memory 可以在线查看,RAM,FLASH,OPTION等

  7. jms异步转同步调用实例

    思路: 当主线程调用异步方法时,将自己挂起,并把引用交给jms的监听: 当监听收到返回的消息时,处理并唤醒主线程继续执行(可以获取和处理返回的消息) Test.java package com.my. ...

  8. Python MySQLdb 批量插入 封装

    def insert_data_many(dbName,list_data_dict): try: # 得到列表的第一个字典集合 data_dict = list_data_dict[0] # 得到( ...

  9. Logstash+ElasticSearch+Kibana处理nginx访问日志(转)

    ELK似乎是当前最为流行的日志收集-存储-分析的全套解决方案. 去年年初, 公司里已经在用, 当时自己还山寨了一个统计系统(postgresql-echarts, 日志无结构化, json形式存储到p ...

  10. ||在oracle数据库中起到字符串拼接的作用

    例子:select org.id from org where inner_code like '12011601001' || '%' ||在oracle数据库中起到字符串拼接的作用,上面等同于'1 ...