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 ...
随机推荐
- python输出缓冲区的问题
碰到的问题,一段代码,print在前,log的在后,查看日志中log的反而在前面.是python输出缓冲区的问题. python输出缓冲区要满 4k 才写入文件,除非禁用缓存或者强制输出或者程序结束. ...
- scheduler定时器相关
定时器官网: http://www.quartz-scheduler.org/
- Linux入门:常用命令:查看硬盘、分区、CPU、内存信息
查看硬盘信息 $df -lh #查看所有硬盘的使用状 $du -sh /etc #查看etc目录大小 #获得文件大小很方便,主要是目录 外部系统挂载 $mount ...
- Java中关于大小写字母的转换
方法一: 大小写字母在ascii码表中相减的值为32 故可将字符串转车字符数组 小写转大写减32 大写转小写加32 String s= "good"; char[] c = s.t ...
- 00007 - Linux时间date命令详解
1.命令:date 2.命令功能:date 可以用来显示或设定系统的日期与时间. 3.命令参数 -d<字符串>:显示字符串所指的日期与时间.字符串前后必须加上双引号: -s<字符串& ...
- confluence部署与破解
一.confluence安装 #安装环境环境 centos7.jdk8.mysql5.7.Confluence6.14.1 confluence下载地址 wget https://product-do ...
- 逻辑运算符&逻辑短路
(1)and 逻辑与 全真则真,一假则假 print(True and True) #True print(False and True) #False print(False and False) ...
- Windows Server 2012 NAT端口转发
- Linux服务器安装Nginx
Nginx 安装 一.安装编译工具及库文件 yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel 二.首先 ...
- css grid 网格布局
前几天研究了一下这个布局方式,笔记待更新 先放一下学习站点 文档 我应该尝试使用CSS Grid Layout的IE实现吗 https://hacks.mozilla.org/2018/02/css- ...