原题链接

题意:

寻找配对的(),并且返回最长可成功配对长度。

思路

配对的()必须是连续的,比如()((),最长长度为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)的更多相关文章

  1. [LeetCode] 32. Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  2. leetcode 32. Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  3. Java [leetcode 32]Longest Valid Parentheses

    题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...

  4. [leetcode]32. Longest Valid Parentheses最长合法括号子串

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  5. LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)

    题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description   Problem :已知字符串s,求出其 ...

  6. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  7. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

  8. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  9. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

随机推荐

  1. 初涉Delphi Socket编程

    不是第一次接触socket编程了,但以前都是看别人的依葫芦画瓢,也不知道具体的原理. 新的项目,有了新的开始,同时也需要有新的认识. Delphi 中带有两套TCP Socket组件: Indy So ...

  2. 【Linux】linux中删除指定日期之前的文件

    要删除系统中就的备份文件,就需要使用命令了: #find /tmp -mtime +30 -type f -name *.sh[ab] -exec rm -f {} \; 假如在一个目录中保留最近30 ...

  3. Spring Cloud Ribbon配置详解

    概述 有时候需要自定义Ribbon的配置和客户端超时配置. 自动化配置 /* 使用属性自定义功能区客户端 从版本1.2.0开始,Spring Cloud Netflix现在支持使用属性与Ribbon文 ...

  4. devexpress GridView按条件给行号上色

    private void gridView1_CustomDrawCell(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDra ...

  5. C++几个细节(1)

    标签: C++ 1. 类的初始化 几种不同初始化的区别: A a;和A *a=new A()的区别,示例程序如下: #include <stdio.h> #include <stri ...

  6. Docker安装MySql-挂载外部数据和配置

    环境 CentOS:7 Docker:1.31.1 MySql:5.7   拷贝mysql配置文件 1.首先创建mysql容器 -p : -e MYSQL\_ROOT\_PASSWORD= -d my ...

  7. canvas 画布基本操作

    const canvas = document.getElementById('canvas'); // 2.画笔 --- canvas的上下文对象 const ctx = canvas.getCon ...

  8. 使用pymysql操作mysql数据库

    PyMySQL的安装和连接 PyMySQL的安装 python3. -m pip install pymysql python连接数据库 import pymysql # 创建连接 conn = py ...

  9. black box黑盒测试

    软件规格说明书 等价类划分,完备性,无冗余性(不能有交集).   健壮等价类:无效等价类 边界值分析,对于一个含有n个变量的程序,采用边界值分析法测试程序会产生4n+1个测试用例           ...

  10. C++屌屌的观察者模式-同步回调和异步回调

    目录 一.概述 1.同步观察者 2.异步观察者 二.效果展示 三.同步观察者 四.异步观察者 五.相关文章 原文链接:C++屌屌的观察者模式-同步回调和异步回调 一.概述 说起观察者模式,也是比较简单 ...