括号匹配
'{([])}', '{}()[]', '([{]])', '[}'
括号匹配检查,匹配返回 true,反之 false
let [str, str1, str2, str3] = ['{([])}', '{}()[]', '([{]])', '[}']
function check(str) {
let stack = [], flag = true
str.split('').map(res => {
if (res === '{' || res === '(' || res === '[') {
stack.push(res)
return
}
let concatStr = stack[stack.length - 1] + res
if (concatStr === '[]' || concatStr === '{}' || concatStr === '()') {
stack.pop()
} else {
flag = false
}
})
return flag
}
console.log(check(str0));
console.log(check(str1));
console.log(check(str2));
console.log(check(str3));