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. OpenStack的HA方案

    一.HA服务分类 HA将服务分为两类: 有状态服务:后续对服务的请求依赖之前对服务的请求,OpenStack中有状态的服务包括MySQL数据库和AMQP消息队列.对于有状态类服务的HA,如neutro ...

  2. <zk在大型分布式系统中的应用>

    Hadoop 在hadoop中,zk主要用来实现HA(High Availability).这部分逻辑主要集中在hadoop common的HA模块中,HDFS的NameNode和Yarn的Resou ...

  3. freckles

    题目描述: In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back ...

  4. python2入门(2)

    四.python条件语句 if语句基本语法if 判断条件: 执行语句块else if: 执行语句块else: 执行语句 五.循环语句 1 - while循环基本语法while 判断条件: 执行语句块w ...

  5. Java多线程入门中几个常用的方法

    一.currentThread()方法 currentThread方法就是返回当前被调用的线程. 该方法为一个本地方法,原码如下: /** * Returns a reference to the c ...

  6. Arcgis属性表出现乱码

    解决方案一:导入符号化字体: 在C:\Windows\Fonts文件夹下放入.TTF格式的字体库(此时加入农村二调_0.TTF和TDT10142007.ttf),便可使符号化的乱码显示正常. 解决方案 ...

  7. [ Codeforces Round #549 (Div. 2)][D. The Beatles][exgcd]

    https://codeforces.com/contest/1143/problem/D D. The Beatles time limit per test 1 second memory lim ...

  8. nodejs----初期学习笔记

    //一 回调函数 //require---命令//Node 使用了大量的回调函数,Node 所有 API 都支持回调函数.//例如,我们可以一边读取文件,一边执行其他命令,在文件读取完成后,我们将文件 ...

  9. robot framework浏览器与驱动的匹配

    一.谷歌浏览器和火狐浏览器与驱动不匹配产生的问题 1.若在运行过程中出现[Unable to find a matching set of capabilities ][ WebDriverExcep ...

  10. poj2228 Naptime【(环结构)线性DP】

    Naptime Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:3374   Accepted: 1281 Descriptio ...