用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
来源:互联网 发布:潇湘馆的竹叶 知乎 编辑:程序博客网 时间:2024/05/24 01:58
import java.util.*;
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.push(node);
}
public int pop() {
if(stack2.empty()){
while(stack1.size()>0){
int tem=stack1.pop();
stack2.push(tem);
}
}
if(stack2.size()==0){
throw new RuntimeException("queue is empty.");
}
return stack2.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.push(node);
}
public int pop() {
if(stack2.empty()){
while(stack1.size()>0){
int tem=stack1.pop();
stack2.push(tem);
}
}
if(stack2.size()==0){
throw new RuntimeException("queue is empty.");
}
return stack2.pop();
}
}
0 0
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 用两个栈来实现一个队列完成队列的Push和Pop操作 队列中的元素为int类型。
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型
- 用两个栈实现队列 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。队列中的元素为int类
- java 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- Android线程池使用心得
- Dockerfile创建自定义Docker镜像以及CMD与ENTRYPOINT指令的比较
- Ajax请求成功后,获取success中的返回的问题
- facebook zencart登录插件
- Web api global Exception Handler
- 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
- 《算法笔记-2》
- CenterOS7.2&Ubuntu14.04配置TURN服务器
- Android Studio提升项目的编译速度
- centos 重启 php-fpm
- 生产者/消费者问题的多种Java实现方式
- Codeforces Round #406 (Div. 2)
- 通过指针、数组,讲静态存储区、栈、堆的区别(转载)/C
- linux shell file 与magic file文件