一、用隊列實現棧
1.1 Question
使用隊列實現棧的下列操作:
push(x) – 元素 x 入棧
pop() – 移除棧頂元素
top() – 獲取棧頂元素
empty() – 返回棧是否爲空
注意:
你只能使用隊列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty
這些操作是合法的。 你所使用的語言也許不支持隊列。 你可以使用 list 或者 deque(雙端隊列)來模擬一個隊列 ,
只要是標準的隊列操作即可。 你可以假設所有操作都是有效的(例如, 對一個空的棧不會調用 pop 或者 top 操作)。
1.2 Analyze
壓入(push)
當我們將一個元素從隊列入隊的時候,根據隊列的性質這個元素會存在隊列的後端。
但當我們實現一個棧的時候,最後入隊的元素應該在前端,而不是在後端。爲了實現這個目的,每當入隊一個新元素的時候,我們可以把隊列的順序反轉過來。
彈出(pop)
最後一個壓入的元素永遠在 q1 的前端,這樣的話我們就能在常數時間內讓它 出隊。
1.3 代碼實現(js)
// 使用隊列實現棧的下列操作:
//
// push(x) -- 元素 x 入棧
// pop() -- 移除棧頂元素
// top() -- 獲取棧頂元素
// empty() -- 返回棧是否爲空
/**
* Initialize your data structure here.
*/
var MyStack = function() {
this.queue = [];
};
/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.queue.unshift(x);
let size = this.queue.length;
while(size>1){
this.queue.unshift(this.queue.pop());
size--;
}
};
/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function() {
return this.queue.pop();
};
/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function() {
return this.queue[this.queue.length - 1];
};
/**
* Returns whether the stack is empty.
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.queue.length === 0;
};
/**
* Your MyStack object will be instantiated and called as such:
* var obj = new MyStack()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.empty()
*/
let st = new MyStack();
st.push(1);
st.push(2);
console.log(st.top());
console.log(st.pop());
二、用棧實現隊列
1.1 Question
使用棧實現隊列的下列操作:
push(x) – 將一個元素放入隊列的尾部。
pop() – 從隊列首部移除元素。
peek() – 返回隊列首部的元素。
empty() – 返回隊列是否爲空。
1.2 Example
MyQueue queue = new MyQueue();
queue.push(1);
queue.push(2);
queue.peek(); // 返回 1
queue.pop(); // 返回 1
queue.empty(); // 返回 false
1.3 Anlyse
1、使用兩個棧,一個棧(stackPush)用於元素進棧,一個棧(stackPop)用於元素出棧;
2、pop() 或者 peek() 的時候:
(1)如果 stackPop 裏面有元素,直接從 stackPop 裏彈出或者 peek 元素;
(2)如果 stackPop 裏面沒有元素,一次性將 stackPush 裏面的所有元素倒入 stackPop。
爲此,可以寫一個 shift 輔助方法,一次性將 stackPush 裏的元素倒入 stackPop。
from:liweiwei1419
1.4 代碼(js)
// 使用棧實現隊列的下列操作:
//
// push(x) -- 將一個元素放入隊列的尾部。
// pop() -- 從隊列首部移除元素。
// peek() -- 返回隊列首部的元素。
// empty() -- 返回隊列是否爲空。
/**
* Initialize your data structure here.
*/
var MyQueue = function() {
this.stackPop = [];
this.stackPush = [];
};
/**
* Push element x to the back of queue.
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
this.stackPush.push(x);
};
/**
* Removes the element from in front of queue and returns that element.
* @return {number}
*/
MyQueue.prototype.pop = function() {
if(this.stackPop.length === 0){
this.poll();
}
return this.stackPop.pop();
};
/**
* Get the front element.
* @return {number}
*/
MyQueue.prototype.peek = function() {
if(this.stackPop.length === 0){
this.poll();
}
return this.stackPop[this.stackPop.length - 1];
};
MyQueue.prototype.poll = function(){
while(this.stackPush.length !== 0){
this.stackPop.push(this.stackPush.pop());
}
};
/**
* Returns whether the queue is empty.
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return this.stackPop.length === 0 && this.stackPush.length === 0;
};
/**
* Your MyQueue object will be instantiated and called as such:
* var obj = new MyQueue()
* obj.push(x)
* var param_2 = obj.pop()
* var param_3 = obj.peek()
* var param_4 = obj.empty()
*/
All from Leetcode