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的更多相关文章

  1. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  4. 【leetcode】 Longest Valid Parentheses (hard)★

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. Longest Valid Parentheses 每每一看到自己的这段没通过的辛酸代码

    Longest Valid Parentheses My Submissions Question Solution  Total Accepted: 47520 Total Submissions: ...

  6. [LeetCode] Longest Valid Parentheses 动态规划

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  7. Java for LeetCode 032 Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. 【Longest Valid Parentheses】cpp

    题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...

  9. Longest Valid Parentheses(最长有效括号)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. AngularJs的UI组件ui-Bootstrap分享(十三)——Progressbar

    进度条控件有两种指令,第一种是uib-progressbar指令,表示单一颜色和进度的一个进度条.第二种是uib-bar和uib-progress指令,表示多种颜色和多个进度组合而成的一个进度条. 这 ...

  2. logsatsh input 插件之 collectd

    logsatsh input 插件之 collectd 标签(空格分隔): logstash 作用:用于监控内存,cpu,磁盘I等信息 未完待续,时间未定. 参考: logstash 官网 elast ...

  3. 修改 Docker 默认网桥地址

    在公司里搭建docker测试环境,需要访问内部的服务, 由于网段是172.17.导致该容器没有办法正常访问公司内部服务.翻了一下官方的帮助文档,找到了修改默认网桥地址的办法. 首先停止正在使用的 Do ...

  4. flyby function

    x=linspace(0.001, 3, 300); y=besselj(5,sqrt(1+x.^2));m=exp(5*i*atan(x.^-1));z=y.*m;plot(x,log(z),'r' ...

  5. java集合类的学习(一)

    为何要用集合类:可以储存不同类型的数据,可以进行动态的删除和修改,不用考虑数组越界的问题. 软件开发常用的集合类:Vector,ArrayList,Stack,HashMap,Hashtable. 3 ...

  6. BaseDao代码,用于连接数据库实行增删改查等操作

    在学习JavaWeb时会用到此代码,用于实行增删改查操作 1 package com.bdqn.dao; import java.sql.Connection; import java.sql.Dri ...

  7. linux 录制并回放终端会话

    发现一个比较好玩的命令,然后这块做一下记录 以下内容复制来源于 LINUX shell 脚本攻略第二版 当你需要为别人在终端上演示某些操作或是需要准备一个命令行教程时,通常得一边手动输入命令一边演示, ...

  8. Java多线程(转)

    文章转自http://286.iteye.com/blog/2292038 谢谢博主的总结! 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位, ...

  9. c# DllImport 找不到指定模块

    两年前的一个项目,基于身份证阅读器的开发,之前都是在公司电脑上开发维护等,今天有需要用到自己的笔记本,只有vs2008和mysql5.5,以为足够,兴致勃勃的拿到客户那里现场解决问题,F5运行程序,程 ...

  10. 每次Xcode 升级之后 插件失效,两步解决

    以下内容来源:http://www.cocoachina.com/bbs/read.php?tid=296269 每次Xcode 升级之后 插件失效,两步解决 1.打开终端,输入以下代码获取到DVTP ...