leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我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的解法的更多相关文章
- leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解
dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...
- 32. Longest Valid Parentheses (Stack; DP)
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
- 【一天一道LeetCode】#32. Longest Valid Parentheses
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 信息安全-加密:RAS 加密
ylbtech-信息安全-加密:RAS 加密 1.返回顶部 1. RSA 是不对称的加密(加密密钥和解密密钥不同 其中 一个为公钥,一个为私钥): 公钥和私钥的产生是基于一对很大的素数(十进制来说 ...
- 学习笔记之Git / Gitflow / TortoiseGit
Git - Wikipedia https://en.wikipedia.org/wiki/Git Git (/ɡɪt/) is a version control system for tracki ...
- [UE4]裁剪 Clipping
Clipping裁剪,是每个UI都有的属性.一般是在容器UI上设置,对容器内的UI进行裁剪. 一.Clip to Bounds:裁剪到边界 二.Clip To Bounds - Without Int ...
- tpadmin的坑收集 nginx下配置tp5失败
如下: 1.ADMIN模块如要关联查询,model的函数名一定不要有“_”下划线,否则找不到 /common/model/**.php 如果把函数名file写成“**_file”,调用时,$vo.** ...
- Zabbix配置参数优化
概述:使用zabbix监控服务器已有一段时间,监控的服务器不到100台,发现刷新zabbix页面有卡顿的现象.而且经常报“Zabbix poller processes more than 75% b ...
- 在Docker中安装配置Oracle11g并实现数据持久化
1.拉取镜像 docker pull registry.cn-hangzhou.aliyuncs.com/helowin/oracle_11g 镜像详情:https://dev.aliyun.com/ ...
- [ERR] Node is not empty. Either the node already knows other nodes (check with C
[root@node00 src]# ./redis-trib.rb add-node --slave --master-id4f6424e47a2275d2b7696bfbf8588e8c4c3a5 ...
- SSH框架总结(环境搭建+框架分析+实例源码下载)
一.SSH框架简介 SSH是struts+spring+hibernate集成的web应用程序开源框架. Struts:用来控制的,核心控制器是Controller. Spring:对Struts和H ...
- 第7章 网络层协议(3)_ARP协议
3. ARP协议 3.1 ARP(Address Resolution Protocol)协议的工作过程和安全隐患 (1)计算机A和C通信之前,先检查ARP缓存中是否有计算机C的IP地址对应的MAC地 ...
- javascript-typeof篇
isString (o) { //是否字符串 return Object.prototype.toString.call(o).slice(8, -1) === 'String' } isNumber ...