LeetCode OJ:Longest Valid Parentheses

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.



struct node
{
    char c;
    int index;
    node(){}
    node(char _c, int _i):c(_c), index(_i){}
};

class Solution {
public:
    int longestValidParentheses(string s) {
        
        stack<node> st;
        st.push(node(')', -1));
        int ret = 0;
        for(int i = 0; i < s.size(); i++)
        {
            char c = s[i];
            if (c == '(')
                st.push(node(c, i));
            else
            {
                node n = st.top();
                if (n.c == '(')
                {
                    st.pop();
                    ret = max(ret, i - st.top().index);
                }
                else
                    st.push(node(c, i));                   
            }
        }
        
        return ret;
    }
};


answer2

动态规划:

f[i]表示i~n最长匹配括号个数,则i~n的形式如()()()

则f[i-1]的个数取决于两种情况:

1、形如 (   ()()()   )中间括号的个数为f[i],则最后一个)match位置为i+f[i]+1,f[i-1]=f[i]+2

2、形如(    )(()()())则I+f[i]+1的位置为粗体所示,但是可以看到f[i-1]个数应该包含后面的(()()())部分,所以f[i-1]还应该加上f[match+1]的值

class Solution {
public:
    int longestValidParentheses(string s) {
        vector<int> f(s.size(),0);
        int ret=0;
        for(int i=s.size()-2;i>=0;--i){
            int match=i+f[i+1]+1;
            //case "(((...)))"
            if(s[i]=='('&&match<s.size()&&s[match]==')'){
                f[i]=f[i+1]+2;
                
                if(match+1<s.size())
                    f[i]+=f[match+1];
            }
            ret=max(ret,f[i]);
        }
        return ret;
    }
};

answer3

两遍扫描:

因为括号只有左括号和右括号两种,对于任意匹配括号()()()(),左括号数目一定等于右括号的数目,所以我们可以设一变量depth,当遇到左括号时++,右括号时--,若为0,则将最大值更新,若是depth<0,说明右括号多了,则将计数起点从下个位置重新开始

但是从前向后扫描只能解决任意位置都是右括号大于或等于左括号个数的问题,但对于形如(()的情况没有考虑到,所以再从后向前扫描一遍

class Solution {
public:
    int longestValidParentheses(string s) {
        int answer=0;
        for(int i=0,start=-1,depth=0;i<s.size();i++){
            if(s[i]=='(')++depth;
            else {
                --depth;
                if(depth<0){
                    start=i;
                    depth=0;
                }else if(depth==0){
                    answer=max(answer,i-start);
                }
            }
        }
        
        for(int i=s.size()-1,end=s.size(),depth=0;i>=0;--i){
            if(s[i]==')')++depth;
            else {
                --depth;
                if(depth<0){
                    end=i;
                    depth=0;
                }else if(depth==0){
                    answer=max(answer,end-i);
                }
            }
        }
        return answer;
    }
};


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值