刷题32. Longest Valid Parentheses
一、题目说明
题目是32. Longest Valid Parentheses,求最大匹配的括号长度。题目的难度是Hard
二、我的做题方法
简单理解了一下,用栈就可以实现。实际上是我考虑简单了,经过5次提交终于正确了。
性能如下:
Runtime: 8 ms, faster than 61.76% of C++ online submissions for Longest Valid Parentheses.
Memory Usage: 9.8 MB, less than 10.71% of C++ online submissions for Longest Valid Parentheses.
代码如下:
#include<iostream>
#include<vector>
#include<stack>
using namespace std;
class Solution {
public:
int longestValidParentheses(string s){
if(s.size()<=1) return 0;
stack<char> st;
vector<int> result;
for(int t=0;t<s.size();t++){
if(s[t]=='('){
st.push(s[t]);
result.push_back(0);
}else if(s[t]==')'){
if(st.empty()){
result.push_back(0);
}else if(st.top() == '('){
st.pop();
if(result.back()>0){
int s = result.size()-1;
//合并所有非 0
while(s>0 && result[s]>0){
s--;
}
if(s < result.size()-2){
int sum = 0,currSize = result.size();
for(int j=s+1;j<currSize-1;j++){
sum += result.back();
result.pop_back();
}
result.back() += sum;
}
int tmp = result.back() + 2;
if(result.size()>0){
result.pop_back();
}
result.back() += tmp;
}else{
result.back() += 2;
}
}
}
}
int max=0,curMax=0;
for(int i=0;i<result.size();i++){
curMax = 0;
int t = i;
while(t<result.size() && result[t]>0){
curMax += result[t];
t++;
}
max = curMax>max ? curMax : max;
}
return max;
}
};
int main(){
Solution s;
cout<<(2==s.longestValidParentheses("(()"))<<endl;
cout<<(4==s.longestValidParentheses(")()())"))<<endl;
cout<<(2==s.longestValidParentheses("()(()"))<<endl;
cout<<(6==s.longestValidParentheses("()(())"))<<endl;
cout<<(4==s.longestValidParentheses("(()()"))<<endl;
cout<<(4==s.longestValidParentheses("(())"))<<endl;
cout<<(6==s.longestValidParentheses("(()())"))<<endl;
cout<<(10==s.longestValidParentheses(")(())(()()))("))<<endl;
cout<<(8==s.longestValidParentheses("((()))())"))<<endl;
return 0;
}
三、优化措施
题解给了4种方法,这4种方法都比较好理解,我上述实现方法属于第3种“栈”,只不过把问题搞复杂了。惭愧!!!
1.暴力法,枚举所有子串,判断合法性,求出最长。
2.动态规划,这一直是我的软肋。
用数组dp表示,其中第 i 个元素表示以下标为 i 的字符结尾的最长有效子字符串的长度。
3.栈
4.不需要额外空间:这种方法非常巧妙,非常考验智商!理解起来不难!
用left和right分别统计左括号和右括号数量,先从左到右统计,遇到左括号left++,遇到右括号right++,如果right==left求最大值,如果left<right则将left=right=0;再从右到左来一遍。
刷题32. Longest Valid Parentheses的更多相关文章
- leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、
20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...
- [Leetcode][Python]32: Longest Valid Parentheses
# -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...
- 32. Longest Valid Parentheses
题目: Given a string containing just the characters '(' and ')', find the length of the longest valid ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- 【LeetCode】32. Longest Valid Parentheses (2 solutions)
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 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 problem 32 -- Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
随机推荐
- java#StringBuffer&StringBuilder
StringBuffer A thread-safe, mutable sequence of characters. A string buffer is like a String, but ca ...
- H7-TOOL脱机烧录器功能开源发布
H7-TOOL汇总帖:https://www.cnblogs.com/armfly/p/12283459.html 当前已经对STM32F030,STM32F103,STM32F429,STM32F7 ...
- gitbook简单教程
简介 GitBook 是一个基于 Node.js 的命令行工具,可使用 Github/Git 和 Markdown 来制作精美的电子书.GitBook支持输出以下几种文档格式 静态站点:GitBook ...
- JS:利用for循环进行数组去重
<script> var a = [1, 3, 2, 4, 5, 3, 2, 1, 4, 6, 7, 7, 6, 6]; //示例数组 var b = []; for(var i = ...
- bean的autowire属性及其生命周期
一:sutowire属性 1.no:默认值,禁用自动装配: 2.byName:根据属性名称自动装配: 3.byType:根据属性类型自动装配: 4.constructor:通过构造方法自动装配,不推荐 ...
- SQL注入过WAF(11.4 第三十三天)
WAF是什么? Web应用防护系统(也称:网站应用级入侵防御系统.英文:Web Application Firewall,简称: WAF).也叫Web防火墙,主要是对Web特有入侵方式的加强防护,如D ...
- 记一次docker使用异常
背景: win10 docker 有几天没有用Oracle数据库,突然发现数据库挂了 docker start oracle 报错 具体错误信息: Error starting userland pr ...
- SciKit-Learn 可视化数据:主成分分析(PCA)
## 保留版权所有,转帖注明出处 章节 SciKit-Learn 加载数据集 SciKit-Learn 数据集基本信息 SciKit-Learn 使用matplotlib可视化数据 SciKit-Le ...
- Petr#(字符串哈希)
CF113B Petr# 大概就是字符串匹配加一个字符串哈希判重.懒得打kmp,就用字符串哈希匹配了. 字符串哈希大概就是把字符串转成一个p进制的数,每一段字符串都有一个对应的哈希值.p尽量取质数,这 ...
- 记-OSPF学习
LSA Type 1:Router LSA1.传播范围 :只能在本区域2.通告者 :每台路由器 (router-id作为标识)3.内容 :路由和拓扑信息show ip ospf database ro ...