题目描述
代码实现:
JS代码实现
var isValid = function(s) {
const stack = new Stack()
const obj = {
')': '(',
']': '[',
'}': '{'
}
for(let v of s){
if(v == '(' || v == '{' || v == '['){
stack.push(v);
}else if(obj[v] == stack.peek()){
stack.pop();
}else{
return false;
}
}
return stack.isEmpty()
};
function Stack(){
this.stack = []
}
Stack.prototype.push = function(ele){
this.stack.push(ele)
}
Stack.prototype.pop = function(){
this.stack.pop()
}
Stack.prototype.peek = function(){
return this.stack[this.stack.length - 1]
}
Stack.prototype.isEmpty = function(){
return this.stack.length == 0
}
参考链接:
- LeetCode20.有效的括号(两种解法)
- 数据结构:栈