zoukankan      html  css  js  c++  java
  • 【LeetCode】32. Longest Valid Parentheses (2 solutions)

    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)第一个留在栈中的括号之前的连续匹配长度。

    (2)最后一个留在栈中的括号之后的连续匹配长度。

    解法一:栈中记录未匹配括号的下标。在全部扫描s之后,将未匹配的括号逐个出栈进行计算。

    class Solution {
    public:
        int longestValidParentheses(string s) {
            int ret = 0;
            stack<int> stk;   //store the indexes of unmatched parentheses
            for(int i = 0; i < s.size(); i ++)
            {
                if(s[i] == '(')
                //unmatch
                    stk.push(i);
                else
                {//s[i] == ')'
                    if(!stk.empty() && s[stk.top()] == '(')
                    //match
                        stk.pop();
                    else
                    //unmatch
                        stk.push(i);
                }
            }
            if(stk.empty())
            //all match
                ret = s.size();
            else
            {//check every unmatched pair of parentheses
                int start;
                int end = s.size()-1;
                while(!stk.empty())
                {
                    start = stk.top();
                    stk.pop();
                    ret = max(ret, end-start);
                    end = start-1;
                }
                //from begin to the first unmatched parenthese
                ret = max(ret, end+1);
            }
            return ret;
        }
    };

    解法二:栈中记录括号及下标。在每次进站时计算与上个未匹配括号的距离。

    struct Par
    {
        char c;
        int ind;
        Par(char newc, int newind): c(newc), ind(newind) {}
    };
    
    class Solution {
    public:
        int longestValidParentheses(string s) {
            if(s == "")
                return 0;
                
            stack<Par> stk;
            int ret = 0;
            int ind;
            for(int i = 0; i < s.size(); i ++)
            {
                if(s[i] == '(')
                {
                    if(!stk.empty())
                    // distance between new unmatched parenthese and last unmatched parenthese
                        ret = max(ret, i-stk.top().ind-1);
                    else
                    // distance between string begin and first unmatched parenthese
                        ret = max(ret, i);
                    Par p(s[i], i);
                    stk.push(p);
                }    
                else if(s[i] == ')')
                {
                    if(!stk.empty())
                    {
                         if(stk.top().c == '(')
                            stk.pop();
                        else
                        {// distance between new unmatched parenthese and last unmatched parenthese
                            ret = max(ret, i-stk.top().ind-1);
                            Par p(s[i], i);
                            stk.push(p);
                        }
                    }
                    else
                    {// distance between string begin and first unmatched parenthese
                        ret = max(ret, i);
                        Par p(s[i], i);
                        stk.push(p);
                    }  
                }
            }
            if(stk.empty())
            {//all matched
                return s.size();
            }
            // distance between string end and last unmatched parenthese
            ret = max(ret, (int)s.size()-stk.top().ind-1);
            
            return ret;
        }
    };

  • 相关阅读:
    高级数据结构实现——自顶向下伸展树
    优先队列——二项队列(binominal queue)
    优先队列——左式堆
    近似装箱问题(两种脱机算法实现)
    近似装箱问题(三种联机算法实现)
    Instruments
    CALayer之 customizing timing of an animation
    PKCS填充方式
    使用Xcode和Instruments调试解决iOS内存泄露
    apple网址
  • 原文地址:https://www.cnblogs.com/ganganloveu/p/4171193.html
Copyright © 2011-2022 走看看