最长有效括号
给你一个只包含 '(' 和 ')' 的字符串,找出最长有效(格式正确且连续)括号子串的长度。
栈的方式
const longestValidParentheses = (s) => {
let max = 0;
const stack = [-1];
for (let i = 0; i < s.length; i++) {
const v = s[i];
if (v === "(") {
stack.push(i);
} else {
stack.pop();
if (stack.length === 0) {
stack.push(i);
} else {
max = Math.max(max, i - stack[stack.length - 1]);
}
}
}
return max;
};
以自己现在的努力程度,还没有资格和别人拼天赋

浙公网安备 33010602011771号