[LeetCode] 32. Longest Valid Parentheses (hard)
原题链接
题意:
寻找配对的(),并且返回最长可成功配对长度。
思路
配对的()必须是连续的,比如()((),最长长度为2;()(),最长长度为4。
解法一 dp:
利用dp记录以s[i]为终点时,最长可配对长度。
仅当遍历到s[i]==’)'的时候记录。
dp[i]=dp[i-1]+2 加上当前组其他对的长度
dp[i]+=dp[i-dp[i]] 加上邻近的上一组的总长度

class Solution {
public:
int longestValidParentheses(string s) {
int len = s.length();
if (len < 2) return 0;
vector<int> dp(len, 0);
int res = 0;
for (int i = 1; i < len; i++) {
if (s[i] == ')') {
if (s[i - 1 - dp[i - 1]] == '(') // 判断当前')'有没有相对应位置的'('
dp[i] = dp[i - 1] + 2; // 如果有:则当前小组数量增加
dp[i] += dp[i - dp[i]]; // 加上上一个小组记录的数量
}
res = max(res, dp[i]);
}
return res;
}
};
解法二 栈:
用栈去存储"(“的索引位置。
遍历字符串,
当前符号为”(“时,加入栈;
当前符号为”)"时,弹出栈顶元素,并进行判断:
——>当栈为空:说明当前所有匹配都匹配成功,长度为i+1;(i从0开始计)
——>当栈不为空:长度为max(answer,i-stack.top())
class Solution {
public:
int longestValidParentheses(string s) {
int res = 0;
int len = s.length();
stack<int> sta;
for (int i = 0; i < len; i++) {
if (!sta.empty() && s[i] == ')' && s[sta.top()] == '(') {
sta.pop();
if (sta.empty())
res = i + 1;
else
res = max(res, i - sta.top());
} else {
sta.push(i);
}
}
return res;
}
};
[LeetCode] 32. Longest Valid Parentheses (hard)的更多相关文章
- [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
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 ...
- LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)
题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description Problem :已知字符串s,求出其 ...
- [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 ...
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- 刷题32. Longest Valid Parentheses
一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...
随机推荐
- Understand the Qt containers(有对应表)
Container classes are one of the cornerstones of object-oriented programming, invaluable tools that ...
- Qt常见皮肤qss代码(有Metro的风格)
##QTabWidget 淡蓝色效果TabWidget(属性值lightblue) QTabWidget[lightblue = "true"] QTabBar::tab{ bor ...
- 系统休眠消息PBT_APMSUSPEND
https://msdn.microsoft.com/en-us/library/windows/desktop/aa372721(v=vs.85).aspx https://msdn.microso ...
- [2017.02.21-22] 《Haskell趣学指南 —— Learning You a Haskell for Great Good!》
{- 2017.02.21-22 <Haskell趣学指南 -- Learning You a Haskell for Great Good!> 学习了Haskell的基本语法,并实现了一 ...
- justgage.js的使用
网址:http://justgage.com/ [1]需引入的文件: <!-- 引入 justGage相关js --><script src="js/raphael-2.1 ...
- 3015C语言_流程设计
第五章 流程设计 5.1 C语句概述 C语言的语句用来向计算机系统发出指令,一个实际的源程序通常包含若干语句,这些语句用来完成一定的操作任务. 1.其他类型语句 函数调用语句(由函数调用加一个分号构成 ...
- Exceptionless(二) - 使用进阶
Exceptionless(二) - 使用进阶 作者:markjiang7m2 原文地址:https://www.cnblogs.com/markjiang7m2/p/11100563.html 官网 ...
- netty服务端启动--ServerBootstrap源码解析
netty服务端启动--ServerBootstrap源码解析 前面的第一篇文章中,我以spark中的netty客户端的创建为切入点,分析了netty的客户端引导类Bootstrap的参数设置以及启动 ...
- 【springBoot】SpringBoot修改启动logo图案
修改boot启动banner logo看到比较好玩,就存一下~ (1)我们在src/main/resources下新建一个banner.txt文件. (2)通过http://patorjk.com/s ...
- 【设计模式】结构型03外观模式(Facade Pattern)
[设计模式]结构型02装饰模式(Decorator Pattern) 意图:为子系统中的一组接口提供一个一致的界面,外观模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 主要解决:降低访问 ...