1. 程式人生 > >劍指offer 05:用兩個棧實現隊列

劍指offer 05:用兩個棧實現隊列

integer solution 棧實現隊列 oid esc urn 實現 scribe stack

題目描述

用兩個棧來實現一個隊列,完成隊列的Push和Pop操作。 隊列中的元素為int類型。

解題代碼

import java.util.Stack;

public class Solution{
    Stack<Integer> in = new Stack<>();
    Stack<Integer> out = new Stack<>();
    
    public void push(int node){
        in.push(node);
    }
    
    public
int pop(){ if(out.isEmpty()) while(!in.isEmpty()) out.push(in.pop()); return out.pop(); } }

劍指offer 05:用兩個棧實現隊列