zoukankan      html  css  js  c++  java
  • 用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。

    题目描述

    用两个栈来实现一个队列,完成队列的Push和Pop操作。 队列中的元素为int类型。
     
    import java.util.Stack;
     
    public class Solution {
        Stack<Integer> stack1 = new Stack<Integer>();
        Stack<Integer> stack2 = new Stack<Integer>();
     
        public void push(int node) {
            int temp = 0;
            if (stack1.isEmpty()) {
                stack1.push(node);
                return;
            }
            while(!stack1.isEmpty()){
                temp=stack1.pop();
                stack2.push(temp);
            }
            stack1.push(node);
            while(!stack2.isEmpty()){
                temp = stack2.pop();
                stack1.push(temp);
            }
           
     
        }
     
        public int pop() {
           if(stack1.isEmpty()){
                 throw new RuntimeException("");
           }
            return stack1.pop();
        }
    }
  • 相关阅读:
    SQL 执行进展优化
    初识SQL 执行顺序
    前端模块化开发的价值(转)
    js 闭包之一
    js模块开发(一)
    简单说说call 与apply
    js 爱恨情仇说 this
    说说 js String
    $Ajax简单理解
    SQL-如何使用 MongoDB和PyMongo。
  • 原文地址:https://www.cnblogs.com/bb3q/p/5084058.html
Copyright © 2011-2022 走看看