目录
1. 用两个栈实现队列
用两个栈来实现一个队列,使用n个元素来完成 n 次在队列尾部插入整数(push)和n次在队列头部删除整数(pop)的功能。 队列中的元素为int类型。保证操作合法,即保证pop操作时队列内已有元素。
import java.util.Stack;
public class Solution {
Stack<Integer> stack1 = new Stack<Integer>();
Stack<Integer> stack2 = new Stack<Integer>();
public void push(int node) {
stack1.add(node);
}
public int pop() {
while(stack1.size()!=0){
int a = stack1.pop();
stack2.add(a);
}
int result = stack2.pop();
while(stack2.size()!=0){
int b = stack2.pop();
stack1.add(b);
}
return result;
}
}
2. 包含min函数的栈
定义栈的数据结构,请在该类型中实