[leetcode] Longest Valid Parentheses

本文详细介绍了解决长有效括号子串问题的三种算法:使用栈和flag数组的方法、使用栈记录长度的方法以及动态规划(DP)方法。每种方法都附有代码实现,旨在帮助读者理解不同策略的实现细节与效率。

from : https://leetcode.com/problems/longest-valid-parentheses/

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

For "(()", the longest valid parentheses substring is "()", which has length = 2.

Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.

思路1:

使用栈和flag数组,每次匹配出栈的时候,将对应的flag位置记录为true。最后在flag中找出最长连续的为true的一段即可。

public class Solution {
    public int longestValidParentheses(String s) {
        int len = s.length();
        if(0 == len || "".equals(s)) return 0;
        Stack<Integer> idxes = new Stack<Integer>();
        boolean flag[] = new boolean[len];
        
        for(int i=0; i<len; ++i) {
            char c = s.charAt(i);
            if(idxes.empty() || '(' == c) {
                idxes.push(i);
            } else if(')' == c) {
                if('(' == s.charAt(idxes.peek())) {
                    flag[i] = flag[idxes.pop()] = true;
                } else {
                    idxes.push(i);
                }
            }
        }
        int max = 0;
        for(int i=0; i<len; ++i) {
            if(flag[i]) {
                int j = i;
                while(i<len && flag[i]) {
                    ++i;
                }
                if(i-j > max) {
                    max = i-j;
                }
            }
        }
        return max;
    }
}

思路2:

用栈,下标本身可以记录长度。

class Solution {
public:
    int longestValidParentheses(string s) {
        int len = s.size(), longest = 0;
        if(0 == len) return 0;
        stack<int> idxes;
        
        for(int i=0; i<len; ++i) {
            char c = s[i];
            if(idxes.empty() || '(' == c) idxes.push(i);
            else if(')' == c) {
                if('(' == s[idxes.top()]) {
                    idxes.pop();
					longest = max(idxes.empty()?i+1:i - idxes.top(), longest);
                } else {
                    idxes.push(i);
                }
            }
        }
   
        return longest;
    }
};

思路3:

DP。

数组dp[i]表示s中从i开始到最后,可以匹配的最长的括号个数。那么,i+dp[i](如果比s.size())就是下一个没有被匹配到的括号。倒着进行dp。

初始值:

dp[n-1] = 0;

递推关系:

如果s[i] == ')', 不可能从它开始有匹配的字符串,故不处理。

如果s[i]=='(',那么找到下一个没有被匹配的位置,j=i+1+dp[i+1],如果这个位置为')',正好匹配一对,那么dp[i] = dp[i+1]+2,且如果下一个位置j的下一个位置(如果j+1<n),那么可以将从i~j和j+1到j+dp[j+1]-1折两段连起来;

class Solution {
public:
    int longestValidParentheses(string s) {
        int len = s.size(), max = 0;
        int *fd = new int[len];
        for(int i=0; i<len; ++i) {
            fd[i] = 0;
        }
        for(int i=len-2; i>=0; --i) {
            if(s[i]=='(') {
                // find indice that i should match
                int j = i+1+fd[i+1];
                if(j < len && s[j] == ')') {
                    fd[i] = fd[i+1]+2;
                    if(j+1 < len && fd[j+1] > 0) {
                        fd[i] += fd[j+1];
                    }
                    if(fd[i] > max) {
                        max = fd[i];
                    }
                }
            }
        }
        delete[] fd;
        return max;
        
    }
};

public class Solution {
    public int longestValidParentheses(String s) {
        int len = s.length(), max = 0;
        int[] fd = new int[len];
        for(int i=len-2; i>=0; --i) {
            if(s.charAt(i)=='(') {
                // find indice that i should match
                int j = i+1+fd[i+1];
                if(j < len && s.charAt(j)==')') {
                    fd[i] = fd[i+1]+2;
                    if(j+1 < len && fd[j+1] > 0) {
                        fd[i] += fd[j+1];
                    }
                    if(fd[i] > max) {
                        max = fd[i];
                    }
                }
            }
            
        }
        return max;
    }
}


评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值