Implement Queue using Stacks

[抄题]:

[思维问题]:

[一句话思路]:

取头部、取出来的时候,用一个output来倒序

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. this.queue = new LinkedList<Integer>(); queue的本质是链表,右边要写成linkedlist

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

[Follow Up]:

[题目变变变]:

class MyQueue {
Stack<Integer> input = new Stack();
Stack<Integer> output = new Stack(); public MyQueue() {
this.input = new Stack<Integer>();
this.output = new Stack<Integer>();
} /** Push element x to the back of queue. */
public void push(int x) {
input.push(x);
} /** Removes the element from in front of queue and returns that element. */
public int pop() {
peek();
return output.pop();
} /** Get the front element. */
public int peek() {
if (output.empty()) {
while (!input.empty()) {
output.push(input.pop());
}
}
return output.peek();
} /** Returns whether the queue is empty. */
public boolean empty() {
if(output.empty() && input.empty()) {
return true;
}
return false;
}
}

Implement Stack using Queues

[抄题]:

[思维问题]:

[一句话思路]:

[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):

[画图]:

[一刷]:

  1. this.queue = new LinkedList<Integer>(); 右边是LinkedList
  2. Queue的push要先把新元素加进去。不然怎么去挤谁呢……
  3. queue的操作语言是poll, add。stack是push/pop
  4. 判断空都写isempty

[二刷]:

  1. 取top时要用peek方法,直接用poll取出来的不一定是最大的
  2. 一开始只需要初始化数据结构,不需要新建对象:Queue<Integer> queue;

[总结]:

[复杂度]:Time complexity: O() Space complexity: O()

[英文数据结构,为什么不用别的数据结构]:

[其他解法]:

[Follow Up]:

[题目变变变]:

class MyStack {
Queue<Integer> queue;
/** Initialize your data structure here. */
public MyStack() {
this.queue = new LinkedList<Integer>();
} /** Push element x onto stack. */
public void push(int x) {
queue.add(x);
for (int i = 0; i < queue.size() - 1; i++) {
queue.add(queue.poll());
}
} /** Removes the element on top of the stack and returns that element. */
public int pop() {
return queue.poll();
} /** Get the top element. */
public int top() {
return queue.peek();
} /** Returns whether the stack is empty. */
public boolean empty() {
if (queue.isEmpty()) {
return true;
}
return false;
}
}

queue,stack的相互实现的更多相关文章

  1. stl容器学习——queue,stack,list与string

    目录 头文件 string 目录部分 1.string的定义及初始化 ① 用一个字符串给另一个字符串赋值 ②用字符串常量对字符串进行赋值 ③ 用n个相同的字符对字符串赋值 2.string的运算符及比 ...

  2. 【Todo】Java Queue Stack Vector ArrayList

    Java集合框架里存在Queue这个接口,之后有不同类型的队列的实现. 有Stack这个类实现堆栈,其实这个类是通过继承Vector的方式来实现的, Vector和ArrayList的实现方式差不多, ...

  3. page74-泛型可迭代的基础集合数据类型的API-Bag+Queue+Stack

    [泛型可迭代的基础集合数据类型的API] 背包:就是一种不支持从中删除元素的集合数据类型——它的目的就是帮助用例收集元素并迭代遍历所有收集到的元素.(用例也可以检查背包是否为空, 或者获取背包中元素的 ...

  4. Part 82 to 85 Talking about Generic queue, stack collection class

    Part 82   Generic queue collection class Part 83   Generic stack collection class Part 84   Real tim ...

  5. Java 集合的简单实现 (ArrayList & LinkedList & Queue & Stack)

    ArrayList 就是数组实现的啦,没什么好说的,如果数组不够了就扩容到原来的1.5倍 实现了迭代器 package com.wenr.collection; import java.io.Seri ...

  6. HashTable Queue Stack SortedList BitArray

    HashTable 由于是非泛型集合,因此存储进去的都是object类型,不管是键还是值. Hashtable不允许排序 key不允许重复 键不允许为null Queue和Queue<T> ...

  7. STL之queue&stack使用简介

       queue 队列也是一个线性存储表,与后进先出的堆栈不同,元素数据的插入在表的一端进行,在另一端删除,从而构成了一个先进先出(First In First Out) 表.插入一端称为队尾,删除一 ...

  8. C++ 数据结构模板 队列 栈 动态链表 模板 Queue Stack List

    C++数据结构模板,可以实现基本功能,用法和stl差不多,比如Q.pop();Q.push(a);Q.front();...... (由于动态链表用的不多,若有错误望各位大神不吝赐教:) 队列: cl ...

  9. CF #366 DIV2 C. Thor 模拟 queue/stack降低复杂度

    C. Thor time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

随机推荐

  1. PostgreSQL下安装pg_stat_statements

    一.安装过程如下:进入postgreSQL安装包的contrib/pg_stat_statements目录,执行编译和安装动作:用root用户 make && make install ...

  2. 解决WPF两个图片控件显示相同图片因线程占用,其中一个显示不全的问题

    在做项目的过程中遇到这样一个问题,下面提出一种解决方法,主要思想是图片的Copy,如还有其他方法,欢迎交流. 在前端图片控件绑定显示时,使用转换器进行转义绑定   (1)转换器: public cla ...

  3. ulimit限制打开的文件数量

    以限制打开文件数为例. ulimit -Hn 查看硬限制. ulimit -Sn 查看软限制. ulimit -n 查看两个中更小的限制(软限制始终比硬限制低, 所以查看的是软限制) 设定规则 1.软 ...

  4. MySQL 删除数据库的两种方法

    使用 mysqladmin 删除数据库 使用普通用户登陆mysql服务器,你可能需要特定的权限来创建或者删除 MySQL 数据库. 所以我们这边使用root用户登录,root用户拥有最高权限,可以使用 ...

  5. springBoot----@ConditionalOnxxx相关注解总结

      下面来介绍如何使用@Condition public class TestCondition implements Condition { /** * 只有返回true,才会启用配置 */ pub ...

  6. UVA-572-搜索基础题

    题意 GeoSurvComp 地理调查公司负责发现石油存储,这次GeoSurvComp公司在一个大型矩形区域上工作,它用一个网格分割地表,然后用可感知装备来单独分析每块小方格区域下是否包含石油,有油的 ...

  7. 4. 纯 CSS 创作一个金属光泽 3D 按钮特效

    原文地址:https://segmentfault.com/a/1190000014599280 HTML代码: <div class="box">BUTTON< ...

  8. smfony设置量表之间的关系

    设置量表之间的关系 验证是否ok 查看我们定义是否有问题 数据库操作 http://www.2cto.com/database/201504/387197.html  设置时间段数据库自动插入时间 不 ...

  9. 4.Spring中使用Log4j

    转自:https://blog.csdn.net/luohai859/article/details/52250807 这里要实现web项目中利用Spring来使用Log4j (1)接上面的工程,然后 ...

  10. 数据库的 ACID 属性

    ACID(Atomicity 原子性.Consistency 一致性.Isolation 隔离性.Durability 持久性)是一系列属性. 这些属性保证了数据库事物的可靠.在数据库中,对数据的一系 ...