zoukankan      html  css  js  c++  java
  • [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.

    思考:无匹配的‘)’为断点。

    class Solution {
    public:
        int longestValidParentheses(string s) {
            // IMPORTANT: Please reset any member data you declared, as
            // the same Solution instance will be reused for each test case.
            int ret=0,last=-1;
            stack<int> left;
            for(int i=0;i<s.size();i++)
            {
                if(s[i]=='(') left.push(i);
                else  //s[i]==')'
                {
                    if(left.empty()) last=i;
                    else
                    {
                        left.pop();
                        if(left.empty()) ret=max(ret,i-last);
                        else ret=max(ret,i-left.top());
                    }
                }
            }
            return ret;
        }
    };
    

      

  • 相关阅读:
    ajax 同步模式与异步模式
    Ajax -get 请求
    Ajax -post 请求
    Ajax 遵循HTTP协议
    Ajax 发送请求
    宽高自适应案例
    伸缩导航案例
    伸缩属性的 grow与 shrink
    伸缩布局
    hdu-5858 Hard problem(数学)
  • 原文地址:https://www.cnblogs.com/Rosanna/p/3447532.html
Copyright © 2011-2022 走看看