/* * 判断括号有效性 */ const str = "{[()]()}"; const valid = (str)=>{ let pool= []; for(let i = 0;i<str.length;i++){ let s = str[i] if(pool.length){ let current = pool[pool.length-1] + s; if(current == '[]' || current == '{}' || current == '()'){ pool.pop(); }else{ pool.push(s) } }else{ pool.push(s) } } console.log(pool) return !Boolean(pool.length); } console.log(valid(str)) // true/false