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 ...
随机推荐
- 设置Windows 7 防火墙端口规则
http://jingyan.baidu.com/article/c843ea0b7d5c7177931e4ab1.html?qq-pf-to=pcqq.c2c 主要解决手机访问pc站点的问题(pc和 ...
- iOS App 获取从后台返回前台时的页面
产品美美的给小伙伴提了一个需求,当程序从后台进入前台时,如果是指定的页面,则弹出提示框. 大家首先想到的方法就是通过 AppDelegate.h 进行控制,相对复杂的步骤就是 在程序进入后台时对当前页 ...
- 7月10日——[HouseStark] 扬帆起航--第一次会议
本次会议为小组成员第一次会议 内容:每个成员提出一个及以上的项目及内容,成员内部商议并投票决定要做的项目 会议时长:90分钟 地点:电三楼8楼816室 成员 项目 讨论结果 崔文祥 高校就业信息汇总网 ...
- 了解JavaScript
JavaScript是什么? JavaScript是一种可以用来给网页增加交互性的编程语言. JavaScript是一种面向对象的语言. JavaScript和Java之间没有太大的关系. JavaS ...
- 关于mongodb的复合索引新功能
最新在做一个项目,由于查询字段较多,且查询较频繁,所以我做了一个复合索引,将所有需要查询的字段都做到索引里,做了一个名为s_1_m_1_c_1_v_1_year_1_month_1_week_1_da ...
- Android中的onActivityResult和setResult方法的使用
如果你想在Activity中得到新打开Activity关闭后返回的数据,你需要使用系统提供的startActivityForResult(Intent intent,int requestCode)方 ...
- http 请求详解大全
HTTP 100 Continue继续 101 Switching Protocols切换协议 200 OK正常 201 Created已创建 202 Accepted已接受 203 Non-Auth ...
- 如何在静态博客hexo中只显示摘要信息
默认情况下hexo博客(如本站)的首页显示的是完整的文章 – 而文章比较长的时候这无疑会带来诸多不遍. 那怎么样才能只显示个摘要呢? 方法说白了,其实很简单 – 只要加入一个<!-- more ...
- LeetCode 20 -- Valid Parentheses
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- 第一章:Android系统移植与驱动开发概述
Android是基于Linux内核的,与Linux内核没有太大的区别,只是增加了一些自己独有的驱动,随着Android发布版本的不断升级,他所使用的Linux内核也在不断升级,以适应新的安卓版本,为他 ...