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.

int longestValidParentheses(string s) {
int maxLen = ;
int lastError = -;
vector<int> stack;
for(int i=; i<s.size(); i++)
{
if (s[i] == '(')
stack.push_back(i);
else if (s[i] == ')')
{
if (stack.size()> )
{
stack.pop_back();
int len;
if (stack.size()==)
len = i - lastError;
else
len = i - stack.back();
if (len > maxLen)
maxLen = len;
}
else
lastError = i;
}
}
return maxLen;
}

最新文章

  1. java GC的优化
  2. PAT 1001. 害死人不偿命的(3n+1)猜想 (15)
  3. divcss5布局
  4. dev/null和dev/zero区别 以及换回设备(loopback device)
  5. Qt中实现启动画面(延时过程中要加上app.processEvents())
  6. 兼容性问题( css)
  7. 【netlimiter】的使用
  8. GMP大法教你重新做人(从入门到实战)
  9. Python入门(青铜篇)
  10. vue.js引入
  11. asp.net core 使用 web deploy 部署网站
  12. Java框架spring 学习笔记(十六):c3p0连接池的配置以及dao使用jdbcTemplate
  13. UVA1103-Ancient Messages(脑洞+dfs)
  14. QT qss 初级介绍
  15. C++实现 safaBase64编码跟nonSafeBase64编码的转换
  16. 开源一个爬取redmine数据的测试报告系统
  17. POJ-3414 Pots (BFS)
  18. include与file_get_contents区别
  19. RDMBorderedButton
  20. CF 1042 F. Leaf Sets

热门文章

  1. MySQL分区(Partition)功能
  2. 安装webpack出现警告: fsevents@^1.0.0 (node_modules\chokidar\node_modules\fsevents):
  3. Python网络爬虫案例(二)——爬取招聘信息网站
  4. C++算法原理与实践(面试中的算法和准备过程)
  5. 20145101《Java程序设计》第4周学习总结
  6. 2018-2019-1 20189218《Linux内核原理与分析》第九周作业
  7. linux内核分析 第五周
  8. 嵌入式C语言--面试题
  9. 星系炸弹|2015年蓝桥杯B组题解析第二题-fishers
  10. 【问题解决】An internal error occurred during: &quot;Computing additional info&quot;. Could not initialize class javax.crypto.JceSecurityManager