zoukankan      html  css  js  c++  java
  • 232. Implement Queue using Stacks

         /*
          * 232. Implement Queue using Stacks
          * 2016-6-14 by Mingyang
          * 只用两个stack,每次只用改push得程序,只要这个每次进来一个,就把所有的依次弹入另一个,加入值以后依次弹回来
          * 这里跟implement stack using queue不一样,不用换queue换来换去的哈
          */
         class MyQueue {
             Stack<Integer> queue = new Stack<Integer>();
             Stack<Integer> temp = new Stack<Integer>();
             // Push element x to the back of queue.
             public void push(int x) {    
                 while (!queue.empty()) {
                     temp.push(queue.pop());
                 }
                 queue.push(x);
                 while (!temp.empty()) {
                     queue.push(temp.pop());
                 }
             }
             // Removes the element from in front of queue.
             public void pop() {
                 queue.pop();
             }
             // Get the front element.
             public int peek() {
                 return queue.peek();
             }
             // Return whether the queue is empty.
             public boolean empty() {
                 return queue.empty();
             }
         }
  • 相关阅读:
    设计模式六大原则
    .net Stream篇(七)
    .net Stream篇(六)
    .net Stream篇(五)
    .net Stream篇(四)
    Leetcode 18
    Leetcode 16
    Leetcode 15
    Leetcode 12
    Leetcode 9
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5586594.html
Copyright © 2011-2022 走看看