zoukankan      html  css  js  c++  java
  • 剑指offer---05---用栈实现队列

    题意
    给了两个栈去实现队列
     
    分析
    两个栈如下情况
          1
          2
    4    3
    这个时候就不能够把4插入到第二个弹出栈了否则弹出顺序出错。
    所以这个时候就应该等第二个栈空了的时候再弹出。
     
    代码
    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.isEmpty()){
                while(!stack1.isEmpty()){
                    int node = stack1.pop();
                    stack2.push(node);
                }
            }
            return stack2.pop();
        }
    }

    转载于:https://www.cnblogs.com/buptyuhanwen/p/9376929.html

  • 相关阅读:
    边框的各种样式
    内容溢出显示省略号
    UNIAPP开发注意事项
    css文本的三条线 删除线 下划线 上划线
    防抖截流
    浏览器窗口改变触发的函数
    ES5数组方法
    弹性布局
    ubuntu16.04 安装adb
    git clone
  • 原文地址:https://www.cnblogs.com/twodog/p/12136365.html
Copyright © 2011-2022 走看看