class Solution {
public:
int longestValidParentheses(string s) {
int n = s.length(), longest = ;
stack<int> st;
for (int i = ; i < n; i++) {
if (s[i] == '(') st.push(i);
else {
if (!st.empty()) {
if (s[st.top()] == '(') st.pop();
else st.push(i);
}
else st.push(i);
}
}
if (st.empty()) longest = n;
else {
int a = n, b = ;
while (!st.empty()) {
b = st.top(); st.pop();
longest = max(longest, a-b-);
a = b;
}
longest = max(longest, a);
}
return longest;
}
};

参考:https://leetcode.com/problems/longest-valid-parentheses/discuss/14126/My-O(n)-solution-using-a-stack

补充一个python的实现:

 class Solution:
def longestValidParentheses(self, s: str) -> int:
n = len(s)
longest = 0
st = []
for i in range(n):
if s[i] == '(':
st.append(i)
else:#s[i] == ')'
if len(st) > 0 and s[st[-1]] == '(':#前一位置是'('
st.pop(-1)#获得一个合法匹配
else:
st.append(i)
if len(st) == 0:#s所有字符都是合法括号对
longest = n
else:
a,b = n,0
while len(st) != 0:#st中记录的都是无法匹配的'('和')'出现的位置,可称为'单身括号'
b = st.pop(-1)
longest = max(longest,a-b-1)#计算当前'单身括号'与下一个'单身括号'的距离,就是最长合法substring(连续子串)的长度
a = b
longest = max(longest,a)
return longest

leetcode32的更多相关文章

  1. LeetCode32 Longest Valid Parentheses

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

  2. [Swift]LeetCode32. 最长有效括号 | Longest Valid Parentheses

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

  3. Leetcode32. 最长有效括号

    32. 最长有效括号 做法 \(f_{i}\)以\(i\)结尾的最长匹配 前提为\(s[i]=')'\) \(s[i-1]='('\),则\(f[i]=f[i-2]+2\) \(s[i-1]=')'\ ...

  4. leetcode32 最长游戏括号 dp

    有一说一,我觉得这题没有到困难级 要保存之前的状态,感觉是很明显的dp 思路和题解一样 class Solution { public: int longestValidParentheses(str ...

随机推荐

  1. python自学第13天 hashlib,re模块

    import hashlib sha=hashlib.sha3_512()#定义加密成什么格式 sha.update('how to use sha1 in 年后 '.encode('utf-8')) ...

  2. Spring Boot 常见标签

    @Controller(value=“名字”,descripation="描述",tags="具体" ) @RestController控制器(path=&qu ...

  3. 网易2018校招内推编程题-堆棋子-C++实现

    链接:https://www.nowcoder.com/questionTerminal/27f3672f17f94a289f3de86b69f8a25b来源:牛客网 [编程题]堆棋子 热度指数:14 ...

  4. 全栈爬取-Scrapy框架(CrawlSpider)

    引入 提问:如果想要通过爬虫程序去爬取”糗百“全站数据新闻数据的话,有几种实现方法? 方法一:基于Scrapy框架中的Spider的递归爬取进行实现(Request模块递归回调parse方法). 方法 ...

  5. U-Boot shell脚本语法

    /********************************************************************** * U-Boot shell脚本语法 * 说明: * 之 ...

  6. C#缓存流的使用浅析

    C#缓存流的使用实例:用缓存流复制文件,C#文件处理操作必须先导入命名空间: using System.IO; ///在按钮的Click事件中添加如下代码: private void button1_ ...

  7. 微信小程序传值以及获取值方法

    http://www.51xuediannao.com/xiaochengxu/xiaochengxu-chuanzhi.html

  8. mongo 索引,速度

    (如有打扰,请忽略)阿里云ECS大羊群,2U4G低至1.4折,限实名新用户,需要的点吧https://promotion.aliyun.com/ntms/act/vm/aliyun-group/tea ...

  9. Ubuntu16.04中pip无法更新升级,采用源码方式安装

    1.从pip官网下载最新版 https://pypi.org/project/pip/#files 2.ubuntu中创建文件位置,我的放在一下路径,之后进行解压 3.解压后进入pip的文件夹,在执行 ...

  10. Python中的闭包 - Closure

    Python中的闭包不是一个一说就能明白的概念,但是随着你往学习的深入,无论如何你都需要去了解这么一个东西. 闭包的概念 我们尝试从概念上去理解一下闭包. 在一些语言中,在函数中可以(嵌套)定义另一个 ...