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. HttpRequestUtil

    package com.didichuxing.tempdirreader; import com.alibaba.fastjson.JSONObject; import java.io.Unsupp ...

  2. output和returnvalue的作用

    贴两段代码. 1> public int ExecuteNonQuery(string pro, MobileOrder or)        {            SqlParameter ...

  3. Win7 64位ORACLE取数字乱码的解决

    参见网址http://www.2cto.com/database/201304/201767.html 首先是PLSQL DEVELOPER 直接报错 NLS_LANG 错误 第一步是在命令行下测试 ...

  4. sqlserver查看所有的外键约束

    select a.name as 约束名, object_name(b.parent_object_id) as 外键表, d.name as 外键列, object_name(b.reference ...

  5. 官网app下载更换成微信公众号二维码 测试

    微信现在很火啊.公司官网原先提供的ios和andriod的app下载链接要求切换成微信公众号二维码.简单的替换,大家都说不需要测试直接上线.还是测了下. 1 验证所有与下载相关的信息都已去除. 包括下 ...

  6. SSH框架

    一,Struts2框架部署 1,搭建Struts2的jar包 1.1,Struts2所需各个jar包的作用 asm-3.3.jar                                  操 ...

  7. IOS设置导航栏字体大小及颜色

    方法一: 自定义视图,定义一个lable,相关属性在lable里设置 核心方法: self.navigationItem.titleView = titleLabel; 方法二:用系统方法直接设置 [ ...

  8. 使用vs2010创建MFC C++ Ribbon程序

    Your First MFC C++ Ribbon Application with Visual Studio 2010 Earlier this month, I put together my ...

  9. android最常用的退出方法

    public class AppUtils extends Application{ private List<Activity> activityList = new LinkedLis ...

  10. 没有为扩展名.htm注册的生成提供程序,没有为扩展名.html注册的生成提供程序

    在web.config中添加下面这段 代码如下 <buildProviders> <add extension=".html" type="System ...