Longest Valid Parentheses
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.
问题描述:给定一个只包含“(”和")"的串,找出一个最长的符合规则的子串。
对于“(()”,最长有效子串是“()”,所以长度是2
另一个例子,“)()())”,最长的有效字串是“()()”,所以长度是4.
解题思路:
(1)申请一个与输入串长度相同的整型数组,初始化值全部为-1,数组和输入串有一一对应的关系;
(2)遍历输入串遇到“(”,就将其对应位置下标入栈;
(3)遇到“)”,就将数组对应位置的值设置为0,弹出栈中第一个值,并将整型数组对应位置置0,这样保证对应的“()”,它们在整型数组中对应的值是0;
(4)遍历结束,寻找0的连续个数最大值,就是要求的结果。

int longestValidParentheses(char* s) {
int slen=strlen(s);
if(slen<=)return ;
int* index=(int*)malloc(sizeof(int)*slen);
for(int i=;i<slen;i++)index[i]=-;
int* stack=(int*)malloc(sizeof(int)*slen);
int top=;
for(int i=;i<slen;i++)
if(s[i]=='(')stack[top++]=i;
else{
if(top!=){
index[stack[top-]]=;
index[i]=;
top--;
}
}
int count=;
int newCount=;
for(int i=;i<slen;i++)
if(index[i]!=-)newCount++;
else{
if(newCount>count){
count=newCount;
}
newCount=;
}
if(newCount>count)count=newCount;
return count;
}
Longest Valid Parentheses的更多相关文章
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码
Longest Valid Parentheses My Submissions Question Solution Total Accepted: 47520 Total Submissions: ...
- [LeetCode] Longest Valid Parentheses 动态规划
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【Longest Valid Parentheses】cpp
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- Longest Valid Parentheses(最长有效括号)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 【转】Tomcat的默认访问路径
放在外网的应用,用户多是直接输入域名访问,相信没有哪个后面还加个尾巴,而Tomcat的默认目录是ROOT,所以我们需要更改其默认目录. 更改Tomcat的默认目录很简单,只需要修改server.xml ...
- #HTML:無序、有序與定義清單
#HTML:無序.有序與定義清單 Maplewing 于 星期六, 12/10/2013 - 09:48 提交 清單在網頁中是很常使用到的東西,故多少還是要了解一下.在HTML中有三種不太一樣的清單, ...
- iOS 判断View 是否是第一次显示
在实现某些需求的时候会有这样的情况,页面第一次加载显示的时候需要某些操作,而以后就不需要重复执行了, 一般这种处理都放在- (void)viewDidLoad或- (id)init因为一般这两个函数除 ...
- 更改mysql中当前auto_increment的值的方法
最近给自己网站更改mysql中当前auto_increment的值 如果在mysql中一个表test中的ID字段设为auto_increment插入两条记录后ID=2,这时删除1条记录,再插入一条变成 ...
- [收藏]谷歌htm/css规范
通用样式规范 协议 省略图片.样式.脚本以及其他媒体文件 URL 的协议部分(http:,https:),除非文件在两种协议下都不可用.这种方案称为 protocol-relative URL,好处是 ...
- js⑦
立即执行函数or自执行函数 为了避免全局变量的产生.(function(){ //var a = 10; //var b = 20;//console.log(a,b); -------------v ...
- The property on could not be set to a 'Int16' value.You must set this property to a non-null value of type ‘Int32’.”
在vs2010 EF4中查询数据总是报错: The property on “XX” could not be set to a 'Int16' value. You must set this p ...
- [SHELL]判断一个命令是否存在
首先要说明的是,不要使用which来进行判断,理由如下: 1.which非SHELL的内置命令,用起来比内置命令的开销大,并且非内置命令会依赖平台的实现,不同平台的实现可能不同. # type typ ...
- SQL列最大重复项
SELECT 1 AS co1, 'a' AS co2 INTO #a UNION SELECT 2, 'a' UNION SELECT 11,'a' UNION SELECT 12, 'a' UNI ...
- 第一次用Axure~
刚刚接触axure感觉好多不会呢~但是一步一步来吧~ 操作到后来发现字体的变化很奇怪,总是只有一个字体出现,只有在编辑时才出现我设定的字体. 但最后还是有个样子出来了~做了一个联系的新页面 最后学姐又 ...