[LeetCode]Longest Valid Parentheses
2013-11-28 13:41
381 查看
Given a string containing just the characters
For
Another example is
思考:无匹配的‘)’为断点。
'('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; } };
相关文章推荐
- windows计算器简单UI设计(java)
- 前Google人谈团队管理:针对不同员工的情境管理法和如何选择合理的团队规模
- ERROR 2003 (HY000): Can't connect to MySQL server
- 页面实时刷新技术探讨
- Java基础3--运算符--判断--循环
- 是做应用还是搞算法?
- - (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCap
- golang实现图片缩放服务器
- Asp.Net 服务器端控件的 ClientIDMode 属性
- 程序员的浮躁症
- Js参数值中含有单引号或双引号解决办法
- 二叉树建立和遍历
- SQL case when then 及 isnull() 例子
- linux系统下如何安装网卡驱动
- 线程控制基本方法
- nyoj_60_谁获得了最高奖学金_201311281117
- 在eclipse中使用Lombok
- 简明Python教程 14)标准库 15)更多内容 16)GUI
- hdu 4495 Rectangle(模拟)
- 用copy只能复制文件,用xcopy却说参数错误?