Skip to content
本页目录

算法题相关

给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效

有效字符串需满足:

  1. 左括号必须用相同类型的右括号闭合。
  2. 左括号必须以正确的顺序闭合。
  3. 每个右括号都有一个对应的相同类型的左括号。
JS
function isValid(s) {
  const stack = [];
  const mapping = {
    ')': '(',
    '}': '{',
    ']': '['
  };

  for (let i = 0; i < s.length; i++) {
    const char = s[i];
    if (mapping[char]) {
      const topElement = stack.length === 0 ? '#' : stack.pop();
      if (mapping[char] !== topElement) {
        return false;
      }
    } else {
      stack.push(char);
    }
  }

  return stack.length === 0;
}
isValid('((({{}}){}))')  //true

如有转载或 CV 的请标注本站原文地址