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. SpringCloud系列------Eureka-Server

    一.概述: Spring Cloud针对服务注册与发现,进行了一层抽象,并提供了三种实现:Eureka , Consul , Zookeeper 本篇文章只对Eureka 进行介绍: (部分内容引用  ...

  2. 面向对象编程其实很简单--python面向对象(初级篇)

    出处:http://www.cnblogs.com/wupeiqi/ 概述 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 面向对象:对函 ...

  3. 二、fread与fwrite

    fread 原型:size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream); 参数: ptr:数据存放地址 size:一个对象的 ...

  4. poi读取excel内容工具类

    该工具类可以读取excel2007,excel2003等格式的文件,xls.xlsx文件格式 package com.visolink; import org.apache.poi.hssf.user ...

  5. 团队-爬虫豆瓣top250项目-模块开发过程

    项目托管平台地址:https://github.com/gengwenhao/GetTop250.git 开发模块功能: "get_info()单个页面的爬取"功能,开发时间:15 ...

  6. 剑指Offer 51. 构建乘积数组 (数组)

    题目描述 给定一个数组A[0,1,...,n-1],请构建一个数组B[0,1,...,n-1],其中B中的元素B[i]=A[0]*A[1]*...*A[i-1]*A[i+1]*...*A[n-1].不 ...

  7. supervisord.conf

    ; Sample supervisor config file.;; For more information on the config file, please see:; http://supe ...

  8. 记一次ssh配置的锅

    我们在使用git来管理代码的时候不可避免的要用到ssh密匙,这个密匙怎么配置的百度上很多. 我这边是使用sourcetree来配合管理代码的,但是我ssh配置好了以后无论是克隆代码还是推送代码都提示我 ...

  9. maven pom.xml配置文件详解

    1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/ ...

  10. 在微信小程序中,如何实现下拉刷新(模拟刷新)

    一.在app.json中启动刷新, 在Windows 中, 添加  "enablePullDownRefresh":"true" 二.在需要刷新的页面中写(若是 ...