zoukankan      html  css  js  c++  java
  • LeetCode-232 Implement Queue using Stacks Solution (with Java)

    1. Description:

    Notes: 

    2. Examples:

    3.Solutions:

     1 /**
     2  * Created by sheepcore on 2019-03-07
     3  * Your MyQueue object will be instantiated and called as such:
     4  * MyQueue obj = new MyQueue();
     5  * obj.push(x);
     6  * int param_2 = obj.pop();
     7  * int param_3 = obj.peek();
     8  * boolean param_4 = obj.empty();
     9  */
    10 class MyQueue {
    11     /** Initialize your data structure here. */
    12     Stack<Integer> input = new Stack<Integer>();
    13     Stack<Integer> output = new Stack<Integer>();
    14 
    15     /** Push element x to the back of queue. */
    16     public void push(int x) {
    17         input.push(x);
    18     }
    19 
    20     /** Removes the element from in front of queue and returns that element. */
    21     public int pop() {
    22         peek();
    23         return output.pop();
    24     }
    25 
    26     /** Get the front element. */
    27     public int peek() {
    28        if(output.empty()){
    29            while (!input.empty()){
    30                output.push(input.pop());
    31            }
    32        }
    33        return output.peek();
    34     }
    35 
    36     /** Returns whether the queue is empty. */
    37     public boolean empty() {
    38         return input.empty() && output.empty();
    39     }
    40 }
  • 相关阅读:
    Android Studio --“Cannot resolve symbol” 解决办法
    js与android webview交互
    关于post与get请求参数存在特殊字符问题
    Fragment 学习笔记(1)
    Android Studio 错误集
    UVA
    UVA
    UVALive
    考试题string——线段树。
    洛谷 1552 [APIO2012]派遣
  • 原文地址:https://www.cnblogs.com/sheepcore/p/12395208.html
Copyright © 2011-2022 走看看