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 ...
随机推荐
- CenOS 6.5下安装docker(转)
2014-12-15 10:23 blessed24 To be Done的博客 字号:T | T 最近在自己的centos上搭建了一个Docker,顺便将一些常用操作记录下. AD:51CTO网+ ...
- java之DatagramSocket、DatagramPackage丶MulticastSocket 广播学习
1.基本概念: a.DatagramPacket与DatagramSocket位于java.net包中 b.DatagramPacket表示存放数据的数据报,DatagramSocket表示接受或发送 ...
- 学习Excel 十大函数
云课堂视频教程 笔记总结: URL:http://study.163.com/course/courseLearn.htm?courseId=1009026#/learn/video?lessonId ...
- 源码编译安装 MySQL 5.5.x 实践
1.安装cmakeMySQL从5.5版本开始,通过./configure进行编译配置方式已经被取消,取而代之的是cmake工具.因此,我们首先要在系统中源码编译安装cmake工具. # wget ht ...
- WPF环境下多点触屏开发的一些经验(转)
本系列将介绍Multi-Touch(MT)多点触控技术的相关内容,使开发人员了解如何在Windows 平台中开发出具有MT 功能的应用程序.众所周知Windows 7 操作系统自身已经支持具有MT 功 ...
- centos 6 SSH配置Google Authentication 验证
创建工作目录: mkdir google-authentication 1. 安装二维码生成依赖 #wget http://fukuchi.org/works/qrencode/qrencode-3. ...
- goldengate studio 12.2.1.2.6发布
主要特性: 1. 支持bigdata & teradata为目标端:
- Python的平凡之路(17)
一.认识jQuery jQuery是一个快速.简洁的JavaScript框架,是继Prototype之后又一个优秀的JavaScript代码库(或JavaScript框架).jQuery设 ...
- VS.net中快捷键收缩和展开代码段 (转)
i. Ctrl-M-O 折叠所有方法 ii. Ctrl-M-P 展开所有方法并停止大纲显示(不可以再折叠了) iii. Ctrl-M-M 折叠或展开当前方法 iv. Ctrl-M-L展开所 ...
- C++中const关键字的使用总结
C++中使用const关键字来修饰常量,下面从两个方面总结:变量和成员函数. 变量:const可以修饰普通变量.指针(数组)和结构体. 1.const修饰普通变量是最简单的情形.这样的用法多为在程序中 ...