We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
是一种运算受限的线性表,后进先出(LIFO)
- push(element): 添加一个新元素到栈顶位置.
push(element)
pop()
peek()
isEmpty()
clear()
size()
// 栈类 function Stack() { // 栈中的属性 var items = [] // 栈相关的方法 // 压栈操作 this.push = function (element) { items.push(element) } // 出栈操作 this.pop = function () { return items.pop() } // peek操作 this.peek = function () { return items[items.length - 1] } // 判断栈中的元素是否为空 this.isEmpty = function () { return items.length == 0 } // 获取栈中元素的个数 this.size = function () { return items.length } }
function test(num){ var stack = new Stack(); var result = 0; while(num>0){ var a = num%2; num = (num-a)/2; stack.push(a) } console.log(stack.get()) while(!stack.isEmpty()){ result += stack.pop() * Math.pow(10, stack.size()) } return result }
var isValid = function (s) { let map = { '(': -1, ')': 1, '[': -2, ']': 2, '{': -3, '}': 3 } let stack = [] for (let i = 0; i < s.length; i++) { if (map[s[i]] < 0) { stack.push(s[i]) } else { let last = stack.pop() if (map[last] + map[s[i]] != 0) return false } } if (stack.length > 0) return false return true };
The text was updated successfully, but these errors were encountered:
No branches or pull requests
栈结构
是一种运算受限的线性表,后进先出(LIFO)
栈结构实现
栈的操作
-
push(element)
: 添加一个新元素到栈顶位置.pop()
:移除栈顶的元素,同时返回被移除的元素。peek()
:返回栈顶的元素,不对栈做任何修改(这个方法不会移除栈顶的元素,仅仅返回它)。isEmpty()
:如果栈里没有任何元素就返回true,否则返回false。clear()
:移除栈里的所有元素。size()
:返回栈里的元素个数。这个方法和数组的length属性很类似。代码实现
应用
参考链接
The text was updated successfully, but these errors were encountered: