一、 用队列实现栈
请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push、top、pop 和 empty)。
实现 MyStack 类:
void push(int x) 将元素 x 压入栈顶。
int pop() 移除并返回栈顶元素。
int top() 返回栈顶元素。
boolean empty() 如果栈是空的,返回 true ;否则,返回 false 。
思路:使用两个队列,保证后添加的元素在队前。先把x添加到临时队列中,如果queue不为空,将queue中的数组push到临时队列中。如果临时队列不为空,就把临时队列中的数据push到queue中,这样循环,就可以保证后来的元素在队伍前面。
var MyStack = function() {
this.queue = [];
this.temp_queue = [];
};
/**
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.temp_queue.push(x);
while(this.queue.length!==0){
this.temp_queue.push(this.queue.shift());
}
while(this.temp_queue.length!==0){
this.queue.push(this.temp_queue.shift())
}
};
/**
* @return {number}
*/
MyStack.prototype.pop = function() {
if(this.queue.length!==0){
return this.queue.shift();
}
};
/**
* @return {number}
*/
MyStack.prototype.top = function() {
if(this.queue.length!==0){
return this.queue[0];
}
};
/**
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.queue.length == 0;
};
二、用栈实现队列
请你仅使用**两个栈**实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):
实现 MyQueue 类:
void push(int x) 将元素 x 推到队列的末尾
int pop() 从队列的开头移除并返回元素
int peek() 返回队列开头的元素
boolean empty() 如果队列为空,返回 true ;否则,返回 false
思路:利用临时栈保证先来的在前面即可
var MyQueue = function() {
this.stack = [];
this.temp_stack = []
};
/**
* @param {number} x
* @return {void}
*/
MyQueue.prototype.push = function(x) {
while(this.stack.length!==0){
this.temp_stack.push(this.stack.shift())
}
this.temp_stack.push(x);
while(this.temp_stack.length!==0){
this.stack.push(this.temp_stack.shift());
}
};
/**
* @return {number}
*/
MyQueue.prototype.pop = function() {
if(this.stack.length!==0){
return this.stack.shift();
}
};
/**
* @return {number}
*/
MyQueue.prototype.peek = function() {
if(this.stack.length!==0){
return this.stack[0];
}
};
/**
* @return {boolean}
*/
MyQueue.prototype.empty = function() {
return this.stack.length == 0;
};
三、 最小栈155
设计一个支持 push ,pop ,top 操作,并能在常数时间内检索到最小元素的栈。
思路: 借用一个辅助min栈保存最小值,每当push一个val时,判断val的值和minStack[this.minStack.length - 1]谁大谁小,谁小把谁push到辅助栈minStack中(注意别忘了等于的情况!!)。pop的时候,把stack和辅助栈中的顶部元素都pop出来。
var MinStack = function() {
this.stack = [];
this.minStack = []
};
/**
* @param {number} val
* @return {void}
*/
MinStack.prototype.push = function(val) {
this.stack.push(val);
if(this.minStack.length === 0){
return this.minStack.push(val)
}
if(val <= this.minStack[this.minStack.length - 1]){
return this.minStack.push(val);
}else if(val > this.minStack[this.minStack.length - 1]){
return this.minStack.push(this.minStack[this.minStack.length - 1])
}
};
/**
* @return {void}
*/
MinStack.prototype.pop = function() {
if(this.stack.length!==0&&this.minStack.length!==0){
this.minStack.pop()
return this.stack.pop()
}
};
/**
* @return {number}
*/
MinStack.prototype.top = function() {
if(this.stack.length!==0){
return this.stack[this.stack.length - 1]
}
};
/**
* @return {number}
*/
MinStack.prototype.getMin = function() {
return this.minStack[this.minStack.length - 1]
};
/**
* Your MinStack object will be instantiated and called as such:
* var obj = new MinStack()
* obj.push(val)
* obj.pop()
* var param_3 = obj.top()
* var param_4 = obj.getMin()
*/
四、poj2545判断是否为合法的出栈序列
思路:出栈结果都存储在队列中,按元素顺序,将元素push入栈。每push一个元素,就检查队列头部与栈元素头部的元素是否相等,如果相等就弹出栈顶元素。若最终的栈为空,说明序列合法,否则不合法。
let arr = [5,1,4,2,3];//出栈结果
var check_is_valid_order = function(arr){
let length = arr.length;
let stack = [];
for(i=1;i<=length;i++){
if(arr[i-1] !== stack[stack.length - 1]){
stack.push(i);
}
while(arr[i-1] === stack[stack.length - 1]){
stack.pop();
}
}
if(stack.length!==0){
return false
}else{
return true;
}
}
console.log(check_is_valid_order(arr))