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

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. 2018牛客多校第六场 I.Team Rocket

    题意: 给出n个区间和m个点(点按顺序给出且强制在线).每个区间只会被第一个他包含的点摧毁.问每个点能摧毁多少个区间以及每个区间是被哪个点摧毁的. 题解: 将n个区间按照左端点排序,然后用vector ...

  2. POJ3304:Segments——题解

    http://poj.org/problem?id=3304 题目大意:给n条线段,求是否存在一条直线,将所有线段投影到上面,使得所有投影至少交于一点. ——————————————————————— ...

  3. 洛谷 P1969 积木大赛 解题报告

    P1969 积木大赛 题目描述 春春幼儿园举办了一年一度的"积木大赛".今年比赛的内容是搭建一座宽度为\(n\)的大厦,大厦可以看成由\(n\)块宽度为1的积木组成,第\(i\)块 ...

  4. de4dot 用法

    使用de4dot反混淆一下 用法 de4dot -f input.dll -o output.dll

  5. 调整KVM虚拟机硬盘大小

    KVM虚拟机的硬盘映像默认存放在“/var/lib/libvirt/images/”中,先查看你的硬盘映像格式是不是RAW格式: qemu-img info /var/lib/libvirt/imag ...

  6. linux查找文件目录及mysql卸载

    我们要卸载 mysql但是不知道其安装在哪里了,可以用  where +关键词 的方式查找,如上图 输入 whereis mysql 后,下面显示出了4个包含mysql的位置. ..... 查看安装m ...

  7. jQuery获取元素对象本身的html

    Jquery获取html标签,包含该标签本身 2018年04月01日 20:16:10 清--水 阅读数:829   版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.c ...

  8. django error: DisallowedHost: Invalid HTTP_HOST header: ''. You may need to add u'' to ALLOWED_HOST

    测试环境: [root@nanx-lli ~]# lsb_release -aLSB Version: :core-4.1-amd64:core-4.1-noarchDistributor ID: C ...

  9. bzoj 1135 [POI2009]Lyz 线段树+hall定理

    1135: [POI2009]Lyz Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 573  Solved: 280[Submit][Status][ ...

  10. [Coding Practice] Maximum number of zeros in NxN matrix

    Question: Input is a NxN matrix which contains only 0′s and 1′s. The condition is no 1 will occur in ...