有一定的难度。用堆栈记录下所有左符的位置,用变量记录下孤立右符的位置。

int longestValidParentheses(const string& s)
{
stack<int>lefts;//将左符对应的位置保留
int last;//记录孤立的右符位置
int max_len = ;//记录当前最长的有效长度
for (int i = ; i < s.size(); i++)
{
if (s[i] == '(')
lefts.push(i);
else
{
if (lefts.empty())
{
last = i;
}
else
{
lefts.pop();
if (lefts.empty())
max_len = max(max_len, i - last);
else
max_len = max(max_len, i - lefts.top());
}
}
} return max_len;
}

Leetcode 之Longest Valid Parentheses(39)的更多相关文章

  1. [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)

    指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...

  2. Java for LeetCode 032 Longest Valid Parentheses

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

  3. LeetCode 之 Longest Valid Parentheses(栈)

    [问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...

  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

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

  6. 【leetcode】Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  7. 【leetcode】 Longest Valid Parentheses (hard)★

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

  8. Java [leetcode 32]Longest Valid Parentheses

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

  9. [leetcode]32. Longest Valid Parentheses最长合法括号子串

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

  10. LeetCode 032 Longest Valid Parentheses

    题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...

随机推荐

  1. 洛谷3690:【模板】Link Cut Tree——题解

    https://www.luogu.org/problemnew/show/P3690 给定n个点以及每个点的权值,要你处理接下来的m个操作.操作有4种.操作从0到3编号.点从1到n编号. 0:后接两 ...

  2. AOJ.850 电缆公司的烦恼 (二分+枚举)

    AOJ.850 电缆公司的烦恼 (二分+枚举) 题意分析 从[1,average]二分枚举长度即可,由于保留2位小数,可以将数据扩大10^2倍后后枚举,输出时除100即可. 代码总览 #include ...

  3. TYVJ2032 升降梯上

    Description: 开启了升降梯的动力之后,探险队员们进入了升降梯运行的那条竖直的隧道,映入眼帘的是一条直通塔顶的轨道.一辆停在轨道底部的电梯.和电梯内一杆控制电梯升降的巨大手柄.Nescafe ...

  4. [LOJ 6159] 最长树链

    看到要求gcd不为1所以肯定在这条答案链上都是一个质数的倍数,所以就会产生一个很暴力的想法 没错,正解就是这样的暴力 只让走是i(素数)倍数的点,作最长链 最长链可以树形dp或两遍bfs,一遍找端点, ...

  5. 解决win10 CPU占用高的问题

    [PConline 技巧]很多笔记本用户在升级到Win10后,都遇到了这样一个问题,那就是Win10的CPU占用明显高于Win7.这个问题对于台式机可能还算不了什么,顶多就是偶尔卡一下罢了.可由于笔记 ...

  6. 【BZOJ5010】【FJOI2017】矩阵填数 [状压DP]

    矩阵填数 Time Limit: 10 Sec  Memory Limit: 128 MB[Submit][Status][Discuss] Description 给定一个 h*w 的矩阵,矩阵的行 ...

  7. 【BZOJ】3038: 上帝造题的七分钟2 && 3211: 花神游历各国

    [算法]线段树||树状数组&&并查集 [题解]修改必须暴力单点修改,然后利用标记区间查询. 优化:一个数经过不断开方很快就会变成1,所以维护区间最大值. 修改时访问到的子树最大值< ...

  8. resultAPI示例

    什么是Restfull API Restfull API 从字面就可以知道,他是rest式的接口,所以就要先了解什么是rest rest 不是一个技术,也不是一个协议 rest 指的是一组架构约束条件 ...

  9. Quick-Cocos2dx-Community_3.6.3_Release 编译时libtiff.lib 无法解析

    Quick-Cocos2dx-Community_3.6.3_Release 使用VS2012编译,报错: libtiff.lib lnk2001 无法解析的外部符号 ltod3 类似于上面这种,刚才 ...

  10. Vuejs - 单文件组件

    为什么需要单文件组件 在之前的实例中,我们都是通过 Vue.component 或者 components 属性的方式来定义组件,这种方式在很多中小规模的项目中还好,但在复杂的项目中,下面这些缺点就非 ...