[LeetCode] 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.

我的思路:

设置matched数组记录每一位是否配对成功

while

{

找到未配对成功的第一个)位于位置i

找到距离i最近的左边的未配对成功的j

配对i和j,设置matched[i]=matched[j]=1

}

找到matched数组中最长的连续1,即所求

class Solution {
public:
    int longestValidParentheses(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        vector<int> matched(s.length(),0);
        
        for(int i=0;i<s.length();i++)
        {
            if(s[i]==')')
            {
                int j=i-1;
                while( j>=0 && !(matched[j]==0 && s[j]=='('))
                {
                    j--;
                }
                if(j>=0)
                {
                    //match i and j
                    matched[i]=matched[j]=1;
                }
            }
        }
        //find the longest consecutive substring 11111... in matched
        int longest=0;
        for(int i=0;i<matched.size();i++)
        {
            int tmplong=0;
            while(i<matched.size() && matched[i]==0) {i++;}
            
            while(i<matched.size() && matched[i]==1)
            {
                tmplong++;
                i++;
            }
            if(tmplong>longest) longest=tmplong;
        }
        return longest;
    }
};
worst case: O(n^2), example: "(((((((((((((((((((((((()))))))))))))))))))))))))"



思路2:

Keep a stack and only store the unmatched "(".


class Solution {
public:
    int longestValidParentheses(string s) {
        // Start typing your C/C++ solution below
        // DO NOT write int main() function
        stack<int> pStack;
        int lastleft=0;//keep track of the start point of the current longest valid parenthese
        int longest=0;
        for(int i=0;i<s.length();i++)
        {
            if(s[i]=='(')
            {
                pStack.push(i);
            }
            else
            {
                if(!pStack.empty())
                {
                    pStack.pop();
                    //now pStack.top() the previous index of the start point of the longest valid parenthese
                    if(!pStack.empty())
                        longest=max(longest,i-pStack.top());
                    else
                        longest=max(longest,i-lastleft+1);
                }
                else{
                    //mismatched ) found, the longest string must end
                    //update the start of the first valid parenthtes 
                    lastleft=i+1;
                }
            }
        }
        return longest;
    }
};


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值