注意!javascript本身function已經足夠,再建一個stack類與prototype添加function不太必要。
下面僅是利用javascript了解stack,因為本身比較熟javascript
stack用在c++,建構list等等,python應該也有
https://juejin.im/post/5b2323896fb9a00e8f795e5b
下面僅是利用javascript了解stack,因為本身比較熟javascript
stack用在c++,建構list等等,python應該也有
https://juejin.im/post/5b2323896fb9a00e8f795e5b
leetcode考題
/**
* Initialize your data structure here.
*/
var MyStack = function() {
this.item=[]
};
/**
* Push element x onto stack.
* @param {number} x
* @return {void}
*/
MyStack.prototype.push = function(x) {
this.item.push(x)
};
/**
* Removes the element on top of the stack and returns that element. 並
* 不是說第一個元素,栈是一种遵从后进先出(LIFO)原则的有序集合,故此處的棧是最後進入的元素
* @return {number}
*/
MyStack.prototype.pop = function() {
return this.item.pop()
};
/**
* Get the top element.
* @return {number}
*/
MyStack.prototype.top = function() {
return this.item[this.item.length-1]
};
/**
* Returns whether the stack is empty.
* @return {boolean}
*/
MyStack.prototype.empty = function() {
return this.item.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()
*/
沒有留言:
張貼留言