原题链接

题意:

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

思路

配对的()必须是连续的,比如()((),最长长度为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. .NET中扩展方法和Enumerable(System.Linq)

    LINQ是我最喜欢的功能之一,程序中到处是data.Where(x=x>5).Select(x)等等的代码,她使代码看起来更好,更容易编写,使用起来也超级方便,foreach使循环更加容易,而不 ...

  2. 基于Bert的文本情感分类

    详细代码已上传到github: click me Abstract:    Sentiment classification is the process of analyzing and reaso ...

  3. 打印第二列为oldboy的第一列内容(awk,grep,sed用法)

    [root@goldtest ~]# cat ip.log 10.0.0.1 oldboy 10.0.0.2 oldgirl 10.0.0.4 tingting 10.0.0.4 oldboy old ...

  4. Prometheus 与 Grafana 集成

    简介 Grafana 是一个可视化仪表盘,它拥有美观的图标和布局展示,功能齐全的仪表盘和图形编辑器,默认支持 CloudWatch.Graphite.Elasticsearch.InfluxDB.My ...

  5. 推荐一个Redis管理工具

    Redis是一个开源(BSD许可),内存存储的数据结构服务器,可用作数据库,高速缓存和消息队列代理.它支持字符串.哈希表.列表.集合.有序集合,位图,hyperloglogs等数据类型.内置复制.Lu ...

  6. node.js简单数据接口开发

    随着网络时代的快速发展,前端开发不仅仅是做出漂亮的页面就可以了,还要会一点后端语言,那么后端语言有Java,php,node.js最常见,那我们应该学哪一种呢,为了让我们自己更好的学习,我推荐选择no ...

  7. composer-laravel-China源和官方源

    composer config -g repo.packagist composer https://repo.packagist.org composer config -g repo.packag ...

  8. Educational Codeforces Round 66 (Rated for Div. 2) A

    A. From Hero to Zero 题目链接:http://codeforces.com/contest/1175/problem/A 题目 ou are given an integer n ...

  9. C# 异步转同步 TaskCompletionSource

    本文通过TaskCompletionSource,实现异步转同步 首先有一个异步方法,如下异步任务延时2秒后,返回一个结果 private static async Task<string> ...

  10. 在 ASP.NET Web API 中使用 Attribute 统一处理异常

    并非所有的异常都需要 try-catch 进行重复的处理,这会导致大量的重复性代码,一旦后续系统出现异常处理机制的修改,随着代码量增多,修改也会变的更加困难. ASP.NET Web API 中特别增 ...