queue,stack的相互实现
Implement Queue using Stacks
[抄题]:
[思维问题]:
[一句话思路]:
取头部、取出来的时候,用一个output来倒序
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:

[一刷]:
- 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
[抄题]:
[思维问题]:
[一句话思路]:
[输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入):
[画图]:
[一刷]:
- this.queue = new LinkedList<Integer>(); 右边是LinkedList
- Queue的push要先把新元素加进去。不然怎么去挤谁呢……
- queue的操作语言是poll, add。stack是push/pop
- 判断空都写isempty
[二刷]:
- 取top时要用peek方法,直接用poll取出来的不一定是最大的
- 一开始只需要初始化数据结构,不需要新建对象: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的相互实现的更多相关文章
- stl容器学习——queue,stack,list与string
目录 头文件 string 目录部分 1.string的定义及初始化 ① 用一个字符串给另一个字符串赋值 ②用字符串常量对字符串进行赋值 ③ 用n个相同的字符对字符串赋值 2.string的运算符及比 ...
- 【Todo】Java Queue Stack Vector ArrayList
Java集合框架里存在Queue这个接口,之后有不同类型的队列的实现. 有Stack这个类实现堆栈,其实这个类是通过继承Vector的方式来实现的, Vector和ArrayList的实现方式差不多, ...
- page74-泛型可迭代的基础集合数据类型的API-Bag+Queue+Stack
[泛型可迭代的基础集合数据类型的API] 背包:就是一种不支持从中删除元素的集合数据类型——它的目的就是帮助用例收集元素并迭代遍历所有收集到的元素.(用例也可以检查背包是否为空, 或者获取背包中元素的 ...
- 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 ...
- Java 集合的简单实现 (ArrayList & LinkedList & Queue & Stack)
ArrayList 就是数组实现的啦,没什么好说的,如果数组不够了就扩容到原来的1.5倍 实现了迭代器 package com.wenr.collection; import java.io.Seri ...
- HashTable Queue Stack SortedList BitArray
HashTable 由于是非泛型集合,因此存储进去的都是object类型,不管是键还是值. Hashtable不允许排序 key不允许重复 键不允许为null Queue和Queue<T> ...
- STL之queue&stack使用简介
queue 队列也是一个线性存储表,与后进先出的堆栈不同,元素数据的插入在表的一端进行,在另一端删除,从而构成了一个先进先出(First In First Out) 表.插入一端称为队尾,删除一 ...
- C++ 数据结构模板 队列 栈 动态链表 模板 Queue Stack List
C++数据结构模板,可以实现基本功能,用法和stl差不多,比如Q.pop();Q.push(a);Q.front();...... (由于动态链表用的不多,若有错误望各位大神不吝赐教:) 队列: cl ...
- 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 ...
随机推荐
- Linux期中架构 全网备份案例
server端脚本 #!/bin/bash #1 进行数据完整性验证 并生成结果 find /backup -type f -name "finger.txt"| xargs md ...
- [UE4]多播代理实例
.h DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FLoginErrorEvent, FString, ErrorMessage); UPROPERTY(B ...
- 本地管理表空间和字典管理表空间的特点,ASSM有什么特点
字典管理表空间(Dictionary-Managed Tablespace简称DMT),8i以前包括以后都还可以使用的一种表空间管理模式,通过数据字典管理表空间的空间使用. Oracle使用两个字典来 ...
- Python3 os模块应用
调用模块的实质是运行python代码,比如a.py文件里有函数f(),那么你在调用a模块的时候,实质是运行了a模块里的函数f(),这个时候内存里就有这个函数了,可以直接用,那是不是随便一个py类型的文 ...
- 《Linux内核精髓:精通Linux内核必会的75个绝技》一HACK #21FUSE
HACK #21FUSE 本节将介绍使用用户进程的文件系统框架—FUSE.FUSE概要FUSE(Filesystem in Userspace,用户空间文件系统),是用来生成用户空间的一般进程的框架. ...
- VFS文件系统结构分析
VFS是Linux非常核心的一个概念,linux下的大部分操作都要用到VFS的相关功能.这里从使用者的角度,对VFS进行了简单说明.使用者不但需要知道Linux下有哪些文件操作的函数,还需要对VFS的 ...
- jshint在bat批处理中闪退,代码中无法调用的问题
先说解决办法:加个call eg: call jshint --version Pause 具体原因有空再更
- JS 传各种文件到后端
由于要写一个前端上传文件按钮功能,本人前端是小白,所以在网上搜索了许多,发现FileReader非常好用. 不多BB,直接来. 1,前端只需要一个input标签, <input type=&qu ...
- IIS6.0 IIS7.5应用程序池自动停止的解决方法 搜集整理
来源:http://www.guchengnet.com/1499.html IIS6.0 IIS7.5应用程序池自动停止的解决方法 搜集整理 发表于2016年12月14日 有2.3个月没有用本地的i ...
- distinct group by
select num from test_test group by num; 比 select distinct(num) from test_test; 效率高 select count(dis ...