Skip to content

leetcode-0032 最长有效括号 #208

@GuoLizhi

Description

@GuoLizhi

var longestValidParentheses = function(s) {
    let maxLen = 0;
    const stack = [-1];
    for (let i = 0; i < s.length; i++) {
        const c = s[i];
        if (c === '(') {
            stack.push(i);
            continue;
        }
        stack.pop();
        if (stack.length === 0) {
            stack.push(i);
        } else {
            maxLen = Math.max(maxLen, i - stack[stack.length - 1]);
        }
    }
    return maxLen;
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions