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.

分析:

求最长合法匹配的长度,这道题可以用一维动态规划逆向求解。假设输入括号表达式为String s,维护一个长度为s.length的一维数组dp[],数组元素初始化为0。 dp[i]表示从s[i]到s[s.length - 1] 包含s[i] 的最长的有效匹配括号子串长度。则存在如下关系:

  • dp[s.length - 1] = 0;
  • i从n - 2 -> 0逆向求dp[],并记录其最大值。若s[i] == '(',则在s中从i开始到s.length - 1计算dp[i]的值。这个计算分为两步,通过dp[i + 1]进行的(注意dp[i + 1]已经在上一步求解):
  • 在s中寻找从i + 1开始的有效括号匹配子串长度,即dp[i + 1],跳过这段有效的括号子串,查看下一个字符,其下标为j = i + 1 + dp[i + 1]。若j没有越界,并且s[j] == ‘)’,则s[i ... j]为有效括号匹配,dp[i] =dp[i + 1] + 2。

    在求得了s[i ... j]的有效匹配长度之后,若j + 1没有越界,则dp[i]的值还要加上从j + 1开始的最长有效匹配,即dp[j + 1]。
class Solution {
public:
int longestValidParentheses(string s) {
int len = s.length();
if(len<2)
return 0;
int max = 0;
int *dp = new int[len];
for(int k = 0;k<len;k++)//把辅助数组清空,存储为0
dp[k] = 0;
for(int i = len-2;i>=0;i--)
{
if(s[i] == '(')//只对左括号处理,右括号在数组中存储为0
{
int j = i+1+dp[i+1];//计算与当前左括号匹配的右括号的位置。可能存在也可能不存在
if(j<len && s[j] == ')')//确保位置不能越界
{
dp[i] = dp[i+1] + 2;//找到了相匹配的右括号,当前数组中存储的最长长度是它后一个位置加2,后一个位置可能存储长度是0
if(j+1<len)//这是连接两个子匹配的关键步骤
dp[i] += dp[j+1];//在j的后面可能已经存在连续的匹配,要记得加上。dp[j+1]存储了以j+1开始的匹配
}
if(dp[i]>max)
max = dp[i];//更新最长长度
} }
return max;
}
};

  其他方法:

stack 并不存字符, 而是存储左括号的位置, 失去匹配的右括号作为分隔符

class Solution {
public:
int ans;
int sum;
int longestValidParentheses(string s) {
ans = sum = 0;
deque<int> stack;
if(s.size() <= 0)
return 0;
int last = -1;
for(int i = 0; i < s.size(); i ++) {
if(s[i] == '(') {
stack.push_back(i);
}else{
if(stack.empty()) {
last = i;
}else{
stack.pop_back();
if(stack.empty()) {
ans = max(ans, i-last);
}else{
ans = max(ans, i-stack.back());
} }
}
}
return ans;
}
};

  

Longest Valid Parentheses(最长有效括号)的更多相关文章

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

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

  2. [leetcode]32. Longest Valid Parentheses最长合法括号子串

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

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

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

  4. 032 Longest Valid Parentheses 最长有效括号

    给一个只包含 '(' 和 ')' 的字符串,找出最长的有效(正确关闭)括号子串的长度.对于 "(()",最长有效括号子串为 "()" ,它的长度是 2.另一个例 ...

  5. 32. Longest Valid Parentheses最长有效括号

    参考: 1. https://leetcode.com/problems/longest-valid-parentheses/solution/ 2. https://blog.csdn.net/ac ...

  6. [Leetcode] longest valid parentheses 最长的有效括号

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

  7. [Swift]LeetCode32. 最长有效括号 | Longest Valid Parentheses

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

  8. LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31

    32. 最长有效括号 32. Longest Valid Parentheses 题目描述 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 每日一算法2019/6/ ...

  9. 32. Longest Valid Parentheses(最长括号匹配,hard)

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

随机推荐

  1. SQL Server表分区案例

    --学习创建表分区脚本/*SQL SERVER 2005中以上版本,终于引入了表分区,就是说,当一个表里的数据很多时,可以将其分拆到多个的表里,大大提高了性能.下面举例子说明之*/ --------- ...

  2. 双栈排序 noip2008

    首先可以看出第一个栈和第二个栈是没什么交集的,那么第一步是对这些元素分别归到两个栈里, 当存在k使i<j<k,a[k]<a[i]<a[j]时,i,j是不能放在一个栈里的,需要一 ...

  3. aspose.cell 自定义模板 SUM无效

    数字类型的单元格, 显示   解决方案: 绑定的DataTable的列为字符串类型. 应该将其设置成数字类型的列

  4. Spring声明式事务配置管理方法(转)

    项目使用SSH架构,现在要添加Spring事务管理功能,针对当前环境,只需要添加Spring 2.0 AOP类库即可.添加方法: 点击项目右键->Build Path->Add libra ...

  5. C# 中的 == 和 equals()有什么区别?

    如以下代码: 1 2 3 4 5 6 7 8 9 int age = 25;   short newAge = 25;   Console.WriteLine(age == newAge);  //t ...

  6. POJ 1700 Crossing River (贪心)

    Crossing River Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 9585 Accepted: 3622 Descri ...

  7. MySQL各个版本区别

    MySQL 的官网下载地址:http://www.mysql.com/downloads/ 在这个下载界面会有几个版本的选择. 1. MySQL Community Server 社区版本,开源免费, ...

  8. CodeIgniter 常量ENVIRONMENT设置要注意的地方

    http://bbs.phpchina.com/thread-274514-1-1.html index.php ,这是CodeIgniter的入口文件,做开发是,都会设置一下define('ENVI ...

  9. android:scaleType属性

    android:scaleType是控制图片如何resized/moved来匹对ImageView的size. ImageView.ScaleType / android:scaleType值的意义区 ...

  10. 动态调用webservice 接口

    1.url:http://localhost:8002/名称.asmx(asmx结尾) 2.需要引用的命名空间:System.Web.Services 3.调用代码: public class Dyn ...