cc150第三章栈队列总结
来源:互联网 发布:网络借贷信息披露细则 编辑:程序博客网 时间:2024/05/16 14:53
3.1用一个array实现3个栈
index i+3即可
3.2使栈可以返回min
1.每个node记录min
2.用新栈存储min
3.3 如果一个栈达到设定的threshold就自动新开一个栈
使用ArrayList或stack记录每个栈.
class SetOfStacks<V>{ Stack<Stack> s = new Stack<Stack>(); private int num=0; SetOfStacks(){ Stack<V> q = new Stack<V>(); s.push(q); } void push(V v){ Stack p = s.peek(); p.push(v); num++; if(num==3){ Stack<V> q = new Stack<V>(); s.push(q); num=0; } } V pop(){ if(s.empty()) System.exit(0); Stack<V> p = s.peek(); V res = p.pop(); if (p.empty()) s.pop(); return res; }}
4to be continued
5.implement a queue using two stacks
push 的时候把B全部导入A,然后PUSH入A,pop的时候把A全部导入B,从B中pop
6.sort a stack by using another stack
使用tmp来记录pop然后对比答案比自己写的简洁
public static void sortStack(Stack<Integer> s){ Stack<Integer> t = new Stack<Integer>(); int tmp = s.pop(); while (!s.empty()){ if(tmp<=s.peek()){ t.push(tmp); tmp=s.pop(); }else{ int tmp2=s.pop(); while (!t.empty()){ s.push(tmp); tmp=t.pop(); if(tmp<=tmp2)break; } t.push(tmp); tmp=tmp2; } } t.push(tmp); while (!t.empty()){ s.push(t.pop()); System.out.println(s.peek()); } }
0 0
- cc150第三章栈队列总结
- 第三章:比较总结栈和队列
- 第三章 栈和队列 总结
- 第三章 栈和队列 总结
- 第三章栈和队列总结
- 《数据结构》第三章 栈和队列 知识总结导图
- 《数据结构》第三章-栈和队列学习总结
- 第三章【栈和队列】 学后总结与心得
- 《数据结构》第三章:栈和队列——>总结
- CC150第14章JAVA总结 to be continued
- 第三章 栈与队列
- 第三章:栈和队列
- 第三章 栈与队列
- 第三章 栈和队列
- 第三章 栈与队列
- 第三章:栈和队列
- 第三章栈和队列
- 第三章 栈和队列
- 三极管的电平转换及驱动电路分析
- hdu3384_双连通分量_
- 错误:java.lang.IllegalArgumentException: attempt to create create event with null entity
- 抓取问题
- 通过批处理命令和计划任务实现程序的停止和监控重启动
- cc150第三章栈队列总结
- C 可变长参数 VS C++11 可变长模板
- linux 大量的TIME_WAIT解决办法
- CodeIgniter 向mysql插入数据包含字母、汉字问题
- 十进制转二进制
- win2003 server + iis6.0 无法播放flv视频解决办法
- JAVA学习篇--javaweb之Filter详解
- 对比C++和java自加问题i++
- openssl DTLS 读写操作代码流程