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. ionic的start-y属性初始化页面

    混合式开发移动app,要实现的是初始化页面时不展示搜索框,让页面向上移动一定位移. <ion-content class="has-subheader" start-y=&q ...

  2. 近期编程问题——read:resource temporarily unavailable

    EAGAIN错误 出现问题:read:resource temporarily unavailable 原因:这种错误一般出现宰非阻塞的socket编程中,资源暂时不可用. 我的解决方法:囧,后来改成 ...

  3. EXCEL里面的数字显示为文本 不用科学计数法显示

    1. 在输入这一串数字前加撇号“'”(英文状态下的单引号)即可.2. 先将这一列设置为“文本”格式,然后直接输入这一串数字即可.   已经输入好了数字,那估计你这些数字的后三位都已经全变成“0”了,用 ...

  4. 如何布局包含Image和Title的UIButton

    UIButton中的titleEdgeInsets和imageEdgeInsets可以管理button中image和title的布局.如果对其理解不够深入,用纯数字进行布局管理,经过不断的调试,还是能 ...

  5. mediawiki安装

    下载wiki,我用的mediawiki-1.23.6 版本. 服务器是window2008 r2 的. 下载xampp,或者wamp都可以,我的用的禅道安装的xampp,解压后直接放在htdocs目录 ...

  6. Python编码问题:UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(12

    今天安装了PyScripter编辑器,刚要写代码,突然就出现异常: <span style="font-size:14px;color:#ff0000;">>&g ...

  7. WordPress博客平台的搭建--基于Ubuntu14服务器

    环境:阿里云服务器,系统Ubuntu14.04, 阿里云域名 大致流程:LNMP+WordPress 主要参考博客:VPS+LNMP+WordPress搭建个人网站/博客 遇到的问题: 1.在登陆域名 ...

  8. 字符数组和string判断是否为空行 NULL和0 namespace变量需要自己进行初始化

    string 可以这样判断空行input !="" 字符数组可以通过判断第一个元素是否为空字符'\0',是的话为空行arrar[0]=='\0':或者用长度strlen(char ...

  9. HttpModule的认识

    1.asp.net的HTTP请求处理过程 说明: (1).客户端浏览器向服务器发出一个http请求,此请求会被inetinfo.exe进程截获,然后转交给aspnet_isapi.dll进程,接着它又 ...

  10. LVM逻辑卷基本概念及LVM的工作原理

    这篇随笔将详细讲解Linux磁盘管理机制中的LVM逻辑卷的基本概念以及LVM的工作原理!!! 一.传统的磁盘管理 其实在Linux操作系统中,我们的磁盘管理机制和windows上的差不多,绝大多数都是 ...