Leetcode 之Longest Valid Parentheses(39)
有一定的难度。用堆栈记录下所有左符的位置,用变量记录下孤立右符的位置。
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)的更多相关文章
- [LeetCode] 032. Longest Valid Parentheses (Hard) (C++)
指数:[LeetCode] Leetcode 指标解释 (C++/Java/Python/Sql) Github: https://github.com/illuz/leetcode 032. Lon ...
- Java for LeetCode 032 Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode 之 Longest Valid Parentheses(栈)
[问题描写叙述] Given a string containing just the characters '(' and ')', find the length of the longest v ...
- [LeetCode] 32. Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- leetcode 32. Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- 【leetcode】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【leetcode】 Longest Valid Parentheses (hard)★
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- Java [leetcode 32]Longest Valid Parentheses
题目描述: Given a string containing just the characters '(' and ')', find the length of the longest vali ...
- [leetcode]32. Longest Valid Parentheses最长合法括号子串
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
- LeetCode 032 Longest Valid Parentheses
题目描述:Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the l ...
随机推荐
- 算法学习 拓扑排序(TopSort)
拓扑排序 一.基本概念 在一个有向无环图(Directed Acyclic Graph, DAG)中,规定< u,v > 表示一条由u指向v的的有向边.要求对所有的节点排序,使得每一条有向 ...
- linux 小技巧
http://blog.csdn.net/xianjie0318/article/details/75712990 1.按内存从大到小排列进程: ps -eo "%C : %p : % ...
- EurekaServer集群配置
一.程序配置 1.pom添加依赖: <dependency> <groupId>org.springframework.cloud</groupId> <ar ...
- HTMLajax跨域向服务器写入数据
1.XMLHttpRequest升级版已经实现了跨域请求.不过需要在后台设置:header("Access-Control-Allow-Origin:http://www.a.com&quo ...
- [linux]ubuntu限速软件
wondersharper 1 安装wondershaper:sudo apt-get install wondershaper2 限制下载,上传速度(1500是限制下载速度(实际限速150k左右), ...
- lightoj 1282 && uva 11029
Leading and Trailing lightoj 链接:http://lightoj.com/volume_showproblem.php?problem=1282 uva 链接:http:/ ...
- SNS应用好友动态Feed模块设计
转载自:http://libo93122.blog.163.com/blog/static/122189382012112145728902/ 备注:找不到原作者了. 现在大部分SNS网站都有一个功能 ...
- CSS3知识之阴影box-shadow
一.定义和用法 box-shadow 属性向框添加一个或多个阴影. box-shadow: h-shadow v-shadow blur spread color inset; h-shadow ...
- Tomcat免安装版+Eclipse配置
Tomcat是目前比较流行的开源且免费的Web应用服务器,在我的电脑上第一次安装Tomcat,再经过网上教程和自己的摸索后,将这个过程 重新记录下来,以便以后如果忘记了可以随时查看. 注意:首先要明确 ...
- 获得WebApi用Post方法获得新增数据的信息
首先,要知道webApi的基本返回方式是HttpResponseMessage,post会在响应中返回添加的对象,以及添加对象的访问地址 如:在fiddler里测试的时候 然后,我们可以根据这一点在后 ...