Shawngbk / Leecode

Questions of Leecode
0 stars 0 forks source link

232. Implement Queue using Stacks #94

Open Shawngbk opened 7 years ago

Shawngbk commented 7 years ago

在添加数据的时候就把数据结构处理好 class MyQueue { Stack que = new Stack<>();

// Push element x to the back of queue.
public void push(int x) {
    Stack<Integer> temp = new Stack<>();
    if(que.isEmpty()) {
        que.push(x);
    } else {
        while(!que.isEmpty()) {
            temp.push(que.pop());
        }
        que.push(x);
        while(!temp.isEmpty()) {
            que.push(temp.pop());
        }
    }
}
// Removes the element from in front of queue.
public void pop() {
    que.pop();
}

// Get the front element.
public int peek() {
    return que.peek();
}

// Return whether the queue is empty.
public boolean empty() {
    return que.isEmpty();
}

}

Shawngbk commented 7 years ago

Microsoft Bloomberg