数据结构与算法练习-Stack栈
来源:互联网 发布:php 数据库连接池 编辑:程序博客网 时间:2024/06/06 03:46
用两个栈实现队列
题目描述
用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
分析
入队:将元素进栈A
出队:判断栈B是否为空,如果为空,则将栈A中所有元素pop,并push进栈B,栈B出栈;
如果不为空,栈B直接出栈。
代码
Stack<Integer> stack1 = new Stack<Integer>(); Stack<Integer> stack2 = new Stack<Integer>(); public void push(int node) { stack1.push(node); } public int pop() { while (!stack2.empty()) { return stack2.pop(); } while (!stack1.empty()) { stack2.push(stack1.pop()); } return stack2.pop(); }
设计Stack(栈)的min函数
描述
定义栈的数据结构,请在该类型中实现一个能够得到栈最小元素的min函数
分析
- 考虑使用一个Integer数min来存储Stack中最小元素,在Stack的push操作的时候,比较和传入值得大小,保持min始终最小。存在问题: 只考虑了入栈操作,出栈操作的时候,min有可能会被pop出去,但是min没有更新。例如依次push 3,2,1 此时min是1,pop一次,最小值应该是2,此时min没有发生变化
- 考虑使用一个栈stack保存数据,用另外一个栈minStack保存依次入栈最小的数
比如:
stack中依次入栈:
3,4,2,5,1
minStack依次入栈 :
3,N,2,N,1
N表示不如栈
每次入栈的时候,如果入栈的元素比minStack中的栈顶元素小或等于则入栈,否则不如栈。
代码
private Stack<Integer> stack = new Stack<Integer>(); private Stack<Integer> minStack = new Stack<Integer>(); private Integer min=null; public void push(int node) { if (min == null) { min = node; minStack.push(min); } else { if (min > node) { min = node; minStack.push(min); } } stack.push(node); } public void pop() { int temp = minStack.pop(); if (temp != stack.pop()) { minStack.push(temp); } } public int top() { int temp = stack.pop(); stack.push(temp); return temp; } public int min() { int temp = minStack.pop(); minStack.push(temp); return temp; }
测试
@Override protected void setUp() throws Exception { super.setUp(); stack = new StackSolution(); stack.push(3); stack.push(4); stack.push(2); stack.push(5); stack.push(1); } @Test public void testMin() { assertEquals(stack.min(), 1); stack.pop();// 1-pop assertEquals(stack.min(), 2); stack.pop(); // 5 pop assertEquals(stack.min(), 2); stack.pop(); // 2 pop assertEquals(stack.min(), 3); stack.pop(); // 4 pop assertEquals(stack.min(), 3); }
判断序列,是否是栈的弹出(pop)序列
描述
输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,3,2,1是该压栈序列对应的一个弹出序列,但4,3,5,1,2就不可能是该压栈序列的弹出序列。
分析
入栈顺序int[] pushA ={1,2,3,4,5}
出栈顺序int[] popA = {4,3,5,1,2}
使用辅助栈 stack,步骤:
- pushA 1入栈,stack-peek:1!= 4 跳过
- pushA 2入栈,stack-peek 2!=4 跳过
- pushA 3入栈,stack-peek 3!=4 跳过
- pushA 4入栈,stack-peek 4==4. 弹出栈顶元素,同时popA索引加1.stack-peek 3 != 5. 跳过
- pushA 5入栈,stack-peek 5==5。 弹出栈顶元素,同时popA索引加1 stack-peek 3==3. 弹出栈顶元素,同时popA索引加1 stack peek 2==2.弹出栈顶元素,同时popA索引加1 stack peek1 == 1。此时stack为空,跳出循环
- 此时没有可以入栈的元素 跳出循环
如果最后辅助栈stack.isEmpty() == true 说明所有元素都出栈,出栈顺序正确
代码
public boolean IsPopOrder(int[] pushA, int[] popA) { if (pushA == null || popA == null || popA.length != pushA.length || pushA.length == 0) { return false; } Stack<Integer> stack = new Stack<Integer>(); int index = 0; for (int i = 0; i < pushA.length; i++) { stack.push(pushA[i]); while (!stack.isEmpty() && popA[index] == stack.peek()) { stack.pop(); index++; } } return stack.isEmpty(); }
测试
@Test public void testIsPopOrder() { int[] array = { 1, 2, 3, 4, 5 }; int[] arrayCorr = { 4, 5, 3, 2, 1 }; int[] arrayError = { 4, 3, 5, 1, 2 }; assertEquals(true, stack.IsPopOrder(array, arrayCorr)); assertEquals(false, stack.IsPopOrder(array, arrayError)); }
0 0
- 数据结构与算法练习-Stack栈
- 数据结构与算法---栈(stack)
- C源码@数据结构与算法->栈Stack
- Java数据结构与算法之stack栈
- 数据结构与算法练习
- 数据结构与算法练习
- 数据结构与算法3: 栈的算法应用(Stack applications)
- 数据结构与算法学习(三)栈结构Stack
- 数据结构与算法分析 P86 Stack 栈的实现
- JS中的算法与数据结构——栈(Stack)
- Data Construction stack 数据结构 栈的练习
- 数据结构与算法练习-递归
- 数据结构与算法练习-排序
- 数据结构与算法分析练习
- 数据结构与算法第一章练习
- 算法与数据结构实验题 3.1 stack
- 数据结构与算法2-栈与队列实现 (stack and queue implementation)
- java数据结构与算法之栈(Stack)设计与实现
- VK Cup 2016 - Round 1 (Div. 2 Edition) C. Bear and Forgotten Tree 3
- 多柱汉诺塔最优算法设计探究
- JAVA基础之——深入理解java多态性
- 由Eclipse导入Android Studio的.9图片报错的处理
- ny 123 士兵杀敌(四)-- 线段树(区间更新,单点查询)
- 数据结构与算法练习-Stack栈
- iOS之动画学习笔记二
- 面向对像编程初步
- URLClassLoader类
- Linux下case报错syntax error near unexpected token `in
- 大搬家 (2016"百度之星" - 测试赛(热身,陈题))
- 安装ubuntukylin后怎样激活root账户
- poj 3067 poj 2481 树状数组变形+对区间排序
- [leetcode] 153. Find Minimum in Rotated Sorted Array