剑指offer
来源:互联网 发布:蛇口招商网络宽带微信 编辑:程序博客网 时间:2024/05/24 05:34
package Chapter2;import java.util.Stack;/** * 面试题7:用两个栈实现队列 * 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。 *//* * 思路: * 一个stack 用来 offer * 一个stack 用poll */public class _07_twoStack {public static void main(String[] args) {_07_function f = new _07_function();f.offer(4);f.offer(6);System.out.println(f.poll());f.offer(10);f.offer(14);System.out.println(f.poll());System.out.println(f.poll());f.offer(16);System.out.println(f.poll());System.out.println(f.poll());}}class _07_function {private Stack<Integer> offer_stack = new Stack<Integer>();private Stack<Integer> poll_stack = new Stack<Integer>();void offer(Integer value) {offer_stack.push(value);}Integer poll() {//if (poll_stack.isEmpty()) {//if(offer_stack.isEmpty()) {//return null;//} else {//while (!offer_stack.isEmpty()) {//poll_stack.push(offer_stack.pop());//}//return poll_stack.pop();//}//} else {//return poll_stack.pop();//}if (poll_stack.isEmpty() && offer_stack.isEmpty()) {return null;}if (poll_stack.isEmpty())while (!offer_stack.isEmpty()) poll_stack.push(offer_stack.pop());return poll_stack.pop();}}
阅读全文
0 0
- 剑指OFFER
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 剑指Offer
- 剑指offer
- 剑指offer
- 《剑指offer》
- 剑指Offer
- 剑指offer
- 剑指offer
- 【一个最最最基本的SSM框架】尽管复制粘贴,maven下搭建框架,很基础,很详细的步骤,框架版的HelloWorld,附数据库数据,spring+springmvc+mybatis
- JetBrains
- "全球程序员节~西安"参会感悟
- 更换python版本后出现 No module named "apt_pkg"
- 地理投影理论知识
- 剑指offer
- 如何解决mongodb在mac上遇到connect failed的问题
- asp.net mvc PC端二维码支付实例(微信二维码支付)
- 说说IO(四)- 文件系统
- 读《大数据时代》
- 彻底了解跨域
- Spring Cloud云服务架构-common-service代码结构分析
- openmp 任务调度 for schedule static dynamic guided runtime
- android的面试知识点总结