刷题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 ...
随机推荐
- C++连接sqlite数据库的坑
新的第一次用vs2013搞 C++连接sqlite数据库,遇到了很多问题,我也不搞不懂~~~下面写点小体会 首先: 你要先配置好sqlite的环境 参考链接: https://blog.csdn.ne ...
- LeetCode1046 最后一块石头的重量(贪心—Java优先队列简单应用)
题目: 有一堆石头,每块石头的重量都是正整数. 每一回合,从中选出两块最重的石头,然后将它们一起粉碎.假设石头的重量分别为 x 和 y,且 x <= y.那么粉碎的可能结果如下: 如果 x == ...
- 2.9 学习总结 之 【Android】体温统计APP
一.说在前面 昨天 学习了JQ的相关知识 今天 编写体温统计APP 我的工程源码:https://github.com/xiaotian12-call/Take-body-temperature 二. ...
- 印度第一颗CPU横空出世!阵势庞大
我们忙着推进国产芯片的同时,隔壁的印度也没闲着.作为印度顶级高校的印度理工学院(IIT)之马德拉斯校区已经发布了其首颗处理器“Shakti”(代表女性力量的印度神话人物)的SDK软件开发包,并承诺会很 ...
- 10 分钟彻底理解 Redis 的持久化和主从复制
在这篇文章,我们继续有关Redis方面知识的学习,一起了解一下其中一个非常重要的内容:Redis的持久化机制. 什么是Redis持久化? Redis作为一个键值对内存数据库(NoSQL),数据都存储在 ...
- Unity UGUI优化整理
看了不少UI优化方面的东西,还是记下来方便记忆,优化性能往往是在各种选择之间做出平衡(空间换时间,或者GPU换CPU,舍弃精度等). 主要优化点在减少Drawcall,减少Overdraw. Mask ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 字体图标(Glyphicons):glyphicon glyphicon-barcode
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name ...
- HTML元素 和 CSS (9.23 第十天)
HTML元素分类:块级元素和内联元素块级元素:标签元素会以新行开始或结束<h1><p><table>等内联元素:显示数据不会以新行开始<a><im ...
- 洛谷 P1470 最长前缀 Longest Prefix
题目传送门 解题思路: 其实思路没那么难,就是题面不好理解,解释一下题面吧. 就是在下面的字符串中找一个子串,使其以某种方式被分解后,每部分都是上面所给集合中的元素. AC代码: #include&l ...
- Vue中 几个常用的命名规范
1,组件名 官方推荐的组件名是 每个单词首字母大写(PascalCase) 或者 全小写用 - 连接(kebab-case) . 在DOM中使用的时候, 改为全小写, 单词之间用 - 连接. Vue. ...