Open asdzxc01 opened 3 years ago
/**
* 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) {
var length = this.queue.length
// 新元素放入队尾
this.queue.push(x)
// 旧元素依次出队重新压入队尾,即反转顺序
while(length--) {
this.queue.push(this.queue.shift())
}
};
/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function() {
return this.queue.shift()
};
/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function() {
return this.queue[ 0 ]
};
/**
* Returns whether the stack is empty.
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.queue.length === 0
};
/**
* 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)
};
/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function () {
return this.queue.shift()
};
/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function () {
return this.queue[0]
};
/**
* 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()
*/
/**
* Initialize your data structure here.
*/
var MyStack = function() {
this.list = []
};
/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.list[this.list.length] = x
};
/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function() {
return this.list.splice(this.list.length - 1, 1)
};
/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function() {
return this.list[this.list.length - 1]
};
/**
* Returns whether the stack is empty.
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return !this.list.length;
};
/**
* 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()
*/
/**
* Initialize your data structure here.
*/
var MyStack = function() {
this.queue = []
this._queue = []
};
/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this._queue.push(x)
while(this.queue.length) {
this._queue.push(this.queue.shift())
}
let swap = this.queue
this.queue = this._queue
this._queue = swap
};
/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function() {
return this.queue.shift()
};
/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function() {
return this.queue[0]
};
/**
* Returns whether the stack is empty.
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return !this.queue.length
};
/**
* 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()
*/
/**
* Initialize your data structure here.
*/
let MyStack = function() {
this.queue = []
// this._queue = []
}
/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.queue.push(x)
}
/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function() {
return this.queue.splice(-1)
}
/**
* 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
}
/**
* Initialize your data structure here.
*/
var MyStack = function() {
this.q = new Array()
};
/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.q.push(x)
};
/**
* Removes the element on top of the stack and returns that element.
* @return {number}
*/
MyStack.prototype.pop = function() {
if (this.q.length > 0) {
return this.q.pop()
}
};
/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function() {
if (this.q.length > 0) {
return this.q[this.q.length - 1]
}
};
/**
* Returns whether the stack is empty.
* @return {boolean}
*/
MyStack.prototype.empty = function() {
if (this.q.length < 1) {
return true
}
return false
};
/**
* 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()
*/
https://leetcode-cn.com/problems/implement-stack-using-queues/