给你一个只包含 '('
和 ')'
的字符串,找出最长有效(格式正确且连续)括号子串的长度。
/** * @param {string} s * @return {number} */ const longestValidParentheses = (s) => { const len = s.length if (len <= 1) return 0 const stack = [-1] let max = 0 for (let i = 0; i < len; i++) { const v = s[i] if (v === '(') { stack.push(i) } else { stack.pop() if (stack.length > 0) { max = Math.max(max, i - stack[stack.length - 1]) } else { stack.push(i) } } } return max };
标签:const,有效,max,len,最长,括号,return,stack From: https://www.cnblogs.com/zhenjianyu/p/17128454.html