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,用栈:1.遍历字符串,当遇到'('的时候,把其索引放入堆栈,当遇到')'时候,如果栈顶是'(',则出栈操作一次,否则索引入栈

   2.当栈为空时候,说明字符串全体合法,返回字符串长度

   3.如果栈不为空,则比较栈中相邻索引的“空洞”长度,最长的空洞即为所求

 class Solution {
public:
int longestValidParentheses(string s) {
int n=s.length(),i,longest=;
stack<int> st;
for(i=;i<n;i++){
if('('==s[i])
st.push(i);
else{
if(!st.empty()){
if(s[st.top()]=='(')
st.pop();
else
st.push(i);
}
else{
st.push(i);
}
}
}
if(st.empty()) longest = n;
else{
int rear=n,front=;
while(!st.empty()){
front=st.top();
st.pop();
longest = max(longest,rear--front);
rear = front;
}
longest = max(longest,front);
}
return longest;
}
};

思路2:动态规划

用longest[]记录字符串中截至每个位置时的最长合法字符串长度

如果s[i]为'(',那么longest[i]为0,因为左右以'('结尾的字符串肯定不合法,为0

如果s[i]为')',那么分两种情况

    1.当s[i-1]为'('时候, longest[i] = longest[i-2] + 2

    2.当s[i-1]为')',和s[i-longest[i-1]-1] == '('时,longest[i] = longest[i-1] + 2 + longest[i-longest[i-1]-2];

      longest[i-longest[i-1]-2]代表上一个以')'结尾的合法字符串

 class Solution {
public:
int longestValidParentheses(string s) {
int len=s.length();
if(len<=)
return ;
int curmax=;
vector<int> longest(len,);
for(int i=;i<len;i++){
if(')'==s[i]){
if('('==s[i-]){
longest[i]=(i->?longest[i-]+:);
curmax=max(longest[i],curmax);
}
else{
if(i-longest[i-]->=&&s[i-longest[i-]-]=='('){
longest[i]=longest[i-]++(i-longest[i-]->?longest[i-longest[i-]-]:);
curmax=max(longest[i],curmax);
}
}
}
}
return curmax;
}
};

     

【LeetCode】32. Longest Valid Parentheses的更多相关文章

  1. 【LeetCode】32. Longest Valid Parentheses (2 solutions)

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  2. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

  3. 【Python】32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  4. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  5. leetcode problem 32 -- Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  6. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  7. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

  8. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

  9. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. 不用注解添加controller抛出No adapter for handler异常

    不用注解添加controller时会抛出No adapter for handler异常. 解决方法:在DispatcherServlet的配置文件(***-servlet.xml)中加入如下两行: ...

  2. Stat3—因子分析(Factor Analysis)

    题注:主成分分析分析与因子分析也有不同,主成分分析仅仅是变量变换,而因子分析需要构造因子模型.主成分分析:原始变量的线性组合表示新的综合变量,即主成分:因子分析:潜在的假想变量和随机影响变量的线性组合 ...

  3. 【BZOJ】3495: PA2010 Riddle 2-SAT算法

    [题意]有n个城镇被分成了k个郡,有m条连接城镇的无向边.要求给每个郡选择一个城镇作为首都,满足每条边至少有一个端点是首都.n,m,k<=10^6. [算法]2-SAT,前后缀优化建图 [题解] ...

  4. python学习笔记(十四)之字典

    字典:是python中唯一的映射类型,字典中每一项都是由键-值对组成的项.字典中没有索引,只有键和值.键的类型可以是整型,变量或字符串. 创建和访问字典: >>> dict1 = { ...

  5. python 版本zabbix_sender

    python版本的zabbix_sender: 使用方法:    1.导入 : from zbx_send import info        2.实例化: test=info()     3.支持 ...

  6. 2017ACM暑期多校联合训练 - Team 7 1002 HDU 6121 Build a tree (深搜+思维)

    题目链接 Problem Description HazelFan wants to build a rooted tree. The tree has n nodes labeled 0 to n− ...

  7. VC拷贝字符串到剪切板

    ] ="中华人民共和国"; DWORD dwLength = ; // 要复制的字串长度 HANDLE hGlobalMemory = GlobalAlloc(GHND, dwLe ...

  8. Dull Chocolates Gym - 101991D 离散化 前缀和

    题目链接:https://vjudge.net/problem/Gym-101991D 具体思路:首先看数据范围,暴力肯定不可以,可以下离散化,然后先求出离散化后每一个点到(1,1)的符合题目的要求的 ...

  9. redis基础之redis-sentinel(哨兵集群)(六)

    前言 redis简单的主从复制在生产的环境下可能是不行的,因为从服务器只能读不能写,如果主服务器挂掉,那么整个缓存系统不能写入了:redis自带了sentinel(哨兵)机制可以实现高可用. redi ...

  10. 64.Minimum Path Sum---dp

    题目链接:https://leetcode.com/problems/minimum-path-sum/description/ 题目大意:从左上到右下的路径中,找出路径和最小的路径(与62,63题相 ...