Longest Valid Parentheses
2015-06-22 02:02
281 查看
Given a string containing just the characters
find the length of the longest valid (well-formed) parentheses substring.
For
which has length = 2.
Another example is
which has length = 4.
'('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) { int n = s.length(); int result = 0; if (n < 2) { return 0; } int buf[n+1]; memset(buf, 0, (n+1)*sizeof(int)); int count = 0; for (int i = 1; i <= n; i++) { if (s[i-1] == '(') { count++; } else if (count > 0) { count--; buf[i] = 2; if (s[i-2] == ')') { buf[i] += buf[i-1]; } buf[i] += buf[i-buf[i]]; if (buf[i] > result) { result = buf[i]; } } } return result; } };
相关文章推荐
- 只看Delphi自带的WnAPI帮助似乎不够
- 彻底成功配置Maven和Eclipse集成
- expdp impdp中 exclude/include 的使用
- 波浪号和Hyphen扩展
- 来北京7天了吧
- SQL Server拾遗
- HDU 5273(递推)
- GDI创建缩略图像
- 11g v$active_session_history的列说明
- JAVA菜鸟入门(17) 有排序的TreeSet和无排序的HashSet, LinkedHashSet
- System.out.println()与System.out.print("\n")的区别
- Docker ubuntu 安装
- HdU5266(LCA + 线段树)
- [资料搜集狂]D3.js数据可视化开发库
- 利用dbms_metadata.get_ddl查看DDL语句
- ImagView 上遮罩一层文字,TextView 最多显示3行,超出省略号
- oracle Hint
- wait_class
- 超屌的自定义多击事件
- 07_Android操作sqllite数据库(包括2中方式操作数据的方式),单元测试,BaseAdapter的使用,自定义view的综合使用案例