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 ...
随机推荐
- locked (a oracle.jdbc.driver.T4CConnection
发现写Oracle的线程挂住了,场景是从mysql读数据,然后写到Oracle. 1 定位线程 因为在同一台机器上运行了多个java进程,要找到对应的pid,就是连接mysql的的那个进程. ...
- 在Ubuntu16.04下面安装asterisk网络电话交换机服务器
在Ubuntu下面需要安装 apt install libncurses5-dev uuid-dev libjansson-dev libxml2-dev libsqlite3-dev 去官网下载包h ...
- windows server 2008 R2 安装
微软服务器操作系统大致有: server 2000(简称2K),还有server 2003(2K3),server 2008(2K8),server 2000和2003是基于NT内核的,而2008是基 ...
- Android Studio启动后出现cannot bind to 127.0.0.1:5037 10048的解决办法
第一次:先连接测试手机,然后启动Android studio时出现下面的弹框,网上查找资料说是360手机助手导致的,但是发现没有安装360手机助手只有360,卸载360后再启动Android stud ...
- 在线学习和在线凸优化(online learning and online convex optimization)—在线分类问题2
紧接上文,我们讲述在线分类问题 令,为0-1损失,我们做出如下的简化假设: 学习者的目标是相对于hypotheses set: H具有low regret,其中H中的每个函数是从到{0,1}的映射,并 ...
- centos7 下安装mysql 关键步骤
#wget https://dev.mysql.com/get/Downloads/MySQL-5.6/mysql-5.6.35-linux-glibc2.5-x86_64.tar.gz 5.7版本下 ...
- 猴哥来了-游戏开发记录17-微信排行榜bug
上线后排行榜bug 1.排序算法 const dataSorter = (gameDatas, field = Consts.OpenDataKeys.LevelKey) => { let d ...
- python requests 发起http POST 请求
python requests 发起http POST 请求,带参数,带请求头: #!/usr/bin/env python # -*- coding: utf-8 -*- import reques ...
- mysql 存储过程动态拼接sql并执行赋值
)) BEGIN ## 定义变量 ,) ; ## @表示全局变量 相当于php $ ## 拼接赋值 INTO 必须要用全局变量不然语句会报错 SET @strsql = CONCAT('SELECT ...
- WPF圆角按钮与触发颜色变化
<Button x:Name="button1" Content="按钮1" Margin="10,10,0,0" Cursor=&q ...