博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
32. Longest Valid Parentheses **堆栈
阅读量:7282 次
发布时间:2019-06-30

本文共 986 字,大约阅读时间需要 3 分钟。

description:

Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

找符合规则的最长的括号
Note:

Example:

Example 1:Input: "(()"Output: 2Explanation: The longest valid parentheses substring is "()"Example 2:Input: ")()())"Output: 4Explanation: The longest valid parentheses substring is "()()"

answer:

class Solution {public:    int longestValidParentheses(string s) {        int res = 0, start = 0;        stack
m; for (int i = 0; i < s.size(); ++i) { if (s[i] == '(') m.push(i); else if (s[i] == ')') { if (m.empty()) start = i + 1; else { m.pop(); res = m.empty() ? max(res, i - start + 1) : max(res, i - m.top()); //取出去之后同样有两种情况讨论 } } } return res; }};

relative point get√:

hint :

转载于:https://www.cnblogs.com/forPrometheus-jun/p/11093031.html

你可能感兴趣的文章
【Todo】OSGi学习
查看>>
MyBatis入门学习教程-优化MyBatis配置文件中的配置
查看>>
JS优秀资源站点收集
查看>>
15 个 Android 通用流行框架大全
查看>>
001_Mac键盘图标与对应快捷按键标志汇总
查看>>
join用法
查看>>
使用JavaScript为一张图片设置备选路径
查看>>
ubuntu selinux
查看>>
discuz回贴通知插件实现-插件后台管理配置
查看>>
spark textFile 困惑与解释
查看>>
除了首付,购房预算还须有这7项才能买到房!
查看>>
Dynamic attention in tensorflow
查看>>
python中的三元运算
查看>>
Swift:宏定义
查看>>
Linux(Ubuntu12.04)上玩儿STC单片机(转)
查看>>
Heroku免费版限制
查看>>
Struts2拦截器
查看>>
推荐电影 梦工厂经典电影列表 1996-2012
查看>>
oc js 调用 函数调用栈
查看>>
ZOJ3951 : Independent Set
查看>>