牛客网—剑指offer-用两个栈实现队列
来源:互联网 发布:大数据工程师工作强度 编辑:程序博客网 时间:2024/06/06 23:19
题目:用两个栈实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
Solution.java
import java.util.Stack;/** * Created by Administrator on 2017/3/1. */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(stack1.empty()&&stack2.empty()){ throw new RuntimeException("Queue is empty!"); } if(stack2.empty()){ while(!stack1.empty()){ stack2.push(stack1.pop()); } } return stack2.pop(); }}
MyTest.java
/** * Created by Administrator on 2017/3/1. */public class MyTest { public static void main(String[] args) { Solution myso = new Solution(); int[] a = {1, 2, 3, 4, 5, 6, 7, 8, 9}; System.out.print("length: "+a.length); for (int i = 0; i < a.length; i++) { myso.push(a[i]); System.out.print("input: "+a[i]); } for(int i=0;i<a.length;i++){ System.out.println(myso.pop()); } }}
0 0
- 牛客网—剑指offer-用两个栈实现队列
- 牛客网—剑指offer-用两个栈实现队列
- [剑指offer]用两个栈实现队列
- 【剑指offer】用两个栈实现队列
- 剑指offer--用两个栈实现队列
- 剑指Offer-用两个栈实现队列
- 剑指offer:用两个栈实现队列
- 《剑指offer》用两个栈实现队列
- 用两个栈实现队列(剑指offer)
- 剑指 offer:用两个栈实现队列
- 【剑指Offer】用两个栈实现队列
- 剑指offer-用两个栈实现队列
- 《剑指offer》-用两个栈实现队列
- 剑指offer 用两个栈实现队列
- 剑指offer 用两个栈实现队列
- [剑指offer]用两个栈实现队列
- 剑指offer--用两个栈实现队列
- 剑指offer|用两个栈实现队列
- 禁止在浏览器中选择和复制黏贴
- Matlab读取图像,并将该图像转换为灰度图像
- PTX ISA 之 原子操作 atom 的小结
- Windows 系统 Github push 或者 下载慢
- 一直以来都没直视的轮播-_-
- 牛客网—剑指offer-用两个栈实现队列
- Android.mk 文件,多个mk文件
- boost::exception
- 树之非递归二叉树先序 中序 后序遍历合集
- Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
- 【ORACLE】报错IMP-00003ORA-02304IMP-00017
- Caused by: java.lang.NoClassDefFoundError: org/springframework/aop/TargetSource
- Java开发中的23种设计模式详解(转)
- [BZOJ 2741]【FOTILE模拟赛】L 分块+可持久trie树