用两个栈实现队列
题目描述
用两个栈来实现一个队列,完成队列的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));
}