第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视..

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.

题目大意:给出一个字符串,给出左右括号完全匹配的最大子串长度。

最开始想着套动态规划,当s[i]..s[j]是完全匹配的,dp[i][j] = j - i +1. 否则dp[i][j] = 0;

然后对各个长度判断。。太笨了。O(n2)的复杂度,超时是必然的,还有bug。

忍不住点开discuss,看了一眼标题,人家是O(n)复杂度,一遍过。惊叹!用的stack。我怎么这么笨呢。。括号匹配、表达式求值就用过stack啊。

懒得琢磨答案的代码,继续自己想。感觉这个题有戏。

在纸上画个字符串模拟,怎么用好一个栈(或者两个栈)。

直接写最后的想法了。中间过程全靠灵感。。

遇到(就压栈。

遇到),取出一个元素。如果取出的是(,匹配了,把()变成数字2,压进去。

如果取出的是数字,继续取,把取出的所有连续数字变成一个长度,直到pop出来的不是数字(字符或者空)。

把最近的左右括号变成2,把相邻的2变成4,把相邻的数字变成更大的和。

然后进行匹配的时候,栈里的数字当做透明的。先全部取出数字,再判断左右括号的匹配。

不管是否匹配,最后要把数字和符号原路压回去。

用字符串演示一下过程:

i = 0和1,左括号入栈

i = 2,s[1]出栈,匹配。变成2压入栈。

i = 3和4, s[3]、s[4]入栈

i = 5,s[4]出栈,变成2压进去。

i = 6,把2取出来,把s[3]取出来,s[3]和s[6]匹配变成2。加上取出来的2,变成4,压进去。

....

最后计算栈里面连续的、不被字符隔断的数字的总和的最大值。

这里遇到一个问题,就是数字 和 () 符号怎么区分。我用了负数来表示。中间出了两次bug,就是有的地方依然用char表示的,负数肯定不能用char了。。

上代码了。。用的C语言,自己写的栈。笨啊   赶紧把STL搞起来!

虽然代码很长,但是好像可读性强一点? 呵呵

struct stack{
int *data;    //存放 括号或者已经匹配的个数(负值)
int n;      //size
int top;
}; typedef struct stack* Stack; Stack new_stack(int n)
{
Stack hd = (Stack)malloc(sizeof(struct stack));
hd->data = (int*)malloc(n*sizeof(int));
hd->top = -;
hd->n = n;
return hd;
} void del_stack(Stack hd)
{
if(hd)
{
if(hd->data) free(hd->data);
free(hd);
}
}
bool is_full(Stack hd)
{
return hd->top == hd->n - ;
} void push(Stack hd, int ch)
{
if( is_full(hd) )
{
int *tmp = (int*)malloc(sizeof(int)*(hd->n + ));
memcpy(tmp,hd->data,hd->n*sizeof(int));
free(hd->data);
hd->data = tmp;
hd->n += ;
} hd->data[++hd->top] = ch;
} int pop(Stack hd)        // 返回值为0表示空栈。
{
if(hd->top < )
return ;
return hd->data[hd->top--];
}
int top(Stack hd)        // 返回值为0表示空栈。
{
if(hd->top < )
return ; return hd->data[hd->top];
} int longestValidParentheses(char* s) {
Stack hd ;
int i,j,k,n = strlen(s);
int cnt,ans;
if(s == NULL || n == )
{
return ;
} hd = new_stack();
for(i=; i<n; i++)
{
if( s[i] == '(' ) // 左侧括号放入
{
push(hd,s[i]);
}
else // 遇到右括号开始处理
{
int ch = pop(hd); // 有4种: '(' ')' 0 负值
if(ch == '(')
{
push(hd,-); //说明匹配了,放进去一个-2代替()
}
else if(ch == ) //表示已经空了。无法匹配了。把)放进去作为间隔
{
push(hd,s[i]);
}
else if(ch < ) //如果之前有匹配好的,
{
while(top(hd)<)
{
ch += pop(hd); //拿出来匹配好的,归并成一个数
}
if(top(hd) == '(' ) //如果是远程匹配,又多了一个2
{
ch += -;
pop(hd);
push(hd,ch);
}
else //如果确定不匹配了,把计算好的匹配数放进去,再把s[i]放进去作为间隔
{
push(hd,ch);
push(hd,s[i]);
}
}
else // 总之是不匹配了
{
push(hd,s[i]);
}
}
}
//计算栈中连续负数的最大值。
ans = cnt = ;
while(hd->top >= )
{
int ch = pop(hd);
if(ch < )
{
cnt -= ch;
if(cnt > ans) ans = cnt;
}
else
{
cnt = ;
}
}
del_stack(hd); return ans;
}

leetcode解题报告 32. Longest Valid Parentheses 用stack的解法的更多相关文章

  1. leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解

    dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...

  2. 32. Longest Valid Parentheses (Stack; DP)

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

  3. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  4. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  5. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

  6. 【一天一道LeetCode】#32. Longest Valid Parentheses

    一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...

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

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

  8. Java [leetcode 32]Longest Valid Parentheses

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

  9. leetcode 32. Longest Valid Parentheses

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

随机推荐

  1. mongodb 的一些基本命令以及 导入、导出,待更新

    基本命令参考: https://blog.csdn.net/cckevincyh/article/details/78702674 导入导出参考:https://blog.csdn.net/djy37 ...

  2. PTA2016天梯赛决赛3小时训练赛

    7-2 I Love GPLT (5 分) 这道超级简单的题目没有任何输入. 你只需要把这句很重要的话 —— I Love GPLT ——竖着输出就可以了. 所谓“竖着输出”,是指每个字符占一行(包括 ...

  3. Centos7修改系统时区timezone

    第一步:查询服务器时间 [root@localhost ~]# timedatectl Local time: Sat 2018-03-31 01:11:46 UTC Universal time: ...

  4. sed初学者实用说明

     转自:http://www.codeweblog.com/sed%E5%88%9D%E5%AD%A6%E8%80%85%E5%AE%9E%E7%94%A8%E8%AF%B4%E6%98%8E/ ...

  5. 解决linux更新apt软件源时报出GPG错误

    今天给树莓派换源,爆出N个这错误: W: GPG error: http://mirrors.neusoft.edu.cn/raspbian/raspbian wheezy InRelease: Th ...

  6. 学习docker后的个人理解

    一.什么是docker Docker 是一个开源的应用容器引擎,基于 Go 语言 并遵从Apache2.0协议开源.可以让开发者打包他们的应用以及依赖包到一个轻量级.可移植的容器中,然后发布到任何流行 ...

  7. Linux性能优化 第五章 性能工具:特定进程内存

    5.1 Linux内存子系统 在诊断内存性能问题的时候,也许有必要观察应用程序在内存子系统的不同层次上是怎样执行的.在顶层,操作系统决定如何利用交换内存和物理内存.它决定应用程序的哪一块地址空间将被放 ...

  8. 7 家 IT 厂商 6394.5 万元中标天津公安云项目(虚拟化、数据库、软件开发)

    http://mp.weixin.qq.com/s/kjum54HJorGTPtZiM-HE1g 天津市公安局云计算平台项目分为:大数据部分.虚拟化部分.数据库部分,软件开发部分,预算分别为:2350 ...

  9. Vuejs自定义全局组件--loading

    不管是使用框架,还是不使用任何的框架,我们都不可避免的需要与“加载中……”打交道,刚刚学习了Vuejs自定义组件的写法,就现学现卖,介绍一下吧! 先看一下目录结构,一般情况下,每一个组件都新建一个新的 ...

  10. LeetCode 4. Median of Two Sorted Arrays & 归并排序

    Median of Two Sorted Arrays 搜索时间复杂度的时候,看到归并排序比较适合这个题目.中位数直接取即可,所以重点是排序. 再来看看治阶段,我们需要将两个已经有序的子序列合并成一个 ...