var longestValidParentheses = function (s) { const stack = [] let result = 0 for (const char of s) { if (char === '(') { stack.push('(') continue } // 下方逻辑处理 char === ')' 时 let sum = 0 while (stack.length > 0 && stack[stack.length - 1] !== '(') { sum += stack.pop() } if (stack.length > 0) { stack.pop() sum += 2 while (stack.length > 0 && stack[stack.length - 1] !== '(') { sum += stack.pop() } stack.push(sum) } result = Math.max(result, sum) } return result }