原题链接

题意:

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

思路

配对的()必须是连续的,比如()((),最长长度为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. Dedecms 中,获取某一栏目所有子栏目

    以前从来没写过递归(其实想想,对算法完全没概念),刚好有这个需求,试着写了一下,发现也挺容易的,特别记录一下. 数据库是dedecms默认的,dede_arctype是保存栏目的表,reid是栏目的父 ...

  2. SQL基础复习2

    一.视图 1.创建视图      创建视图后加 WITH CHECK OPTION 2.视图查询 数据库系统的处理方法: 视图消解法(View Resolution) 步骤: 进行有效性检查-> ...

  3. Google C++测试框架系列高级篇:第一章 更多关于断言的知识

    原始链接:More Assertions 词汇表 现在你应该已经读完了入门篇并且会使用GTest来写测试.是时候来学一些新把戏了.这篇文档将教会你更多知识:用断言构造复杂的失败信息,传递致命失败,重用 ...

  4. python字典的内建函数

    In [70]: test=dict(x=1,y=2,z=3) In [71]: test Out[71]: {'x': 1, 'y': 2, 'z': 3} In [72]: a=['a','b', ...

  5. Dynamics 365中的事件框架与事件执行管道(Event execution pipeline)

    本文介绍了Microsoft Dynamics 365(以下简称D365)中的两个概念,事件框架(Event Framework)与事件执行管道(Event execution pipeline). ...

  6. 查看linux系统时间和时区

    参考地址:http://lidao.blog.51cto.com/ 一.使用date命令查看系统时间 [root@benbang ~]# date -R Tue, 01 Aug 2017 15:43: ...

  7. while循环语句、格式化输出、常用运算符、字符编码

    1.while循环 while 空格 条件 冒号 缩进 循环体 num=1 while num<11: print(num) num=num+1 变量都是先执行等号右边的,然后执行等号左边的. ...

  8. 前端笔记之React(三)使用动态样式表&antd&React脚手架&props实战

    一.使用动态样式表 1.1 LESS使用 全局安装Less npm install -g less 创建1.less文件,然后可以用lessc命令来编译这个文件: lessc 1.less 1.css ...

  9. 谈谈NOSQL

    从MongoDB引到NOSQL 要讲MongoDB之前,首先要提到一个概念NOSQL(NoSQL = Not Only SQL ) 很大一部分数据是由关系型数据库管理系统(RDMBSs)来处理的,关系 ...

  10. java 学习之路第一节

    一.安装mysql驱动:http://mvnrepository.com/search?q=mysql-conn 二.看数据库中有什么表安装:navicat for MySQL 文件类写数据库查询: ...