leetcode第31题--Longest Valid Parentheses
Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
For "(()", the longest valid parentheses substring is "()", which has length = 2.
Another example is ")()())", where the longest valid parentheses substring is "()()", which has length = 4.
给定一串只包含左右括号的字符串,判断里面合法的个数。左右括号镶嵌嵌套也是合法的例如(())(),合法的和合法的相连也是合法的。这个用DP是可以的。在http://www.cnblogs.com/huntfor/p/3886111.html中介绍了DP可以通过。在http://www.cnblogs.com/lichen782/p/leetcode_Longest_Valid_Parentheses.html的DP却说通不过。改天要好好复习一下动态规划。
我学习了如下的解法。就是按照从前往后遍历,遇到左括号就放入栈中,栈是用来存左括号的下标的,方便之后计算长度。遇到右括号的话考虑两种情况,如果此时栈为空,那这个右括号就是违法的,用last记录它的位置,这样last就记录了我们遍历到i为止的最后一个违法的数,这样的话,再往下遍历,如果发现栈为空了的时候就可以用i-last来表示刚才在i和last中间合法的个数。如果往下遍历发现栈不为空,那么就用当前的i-栈顶元素,因为栈顶元素记录的是离i最近的一个左括号,所以i减去它就记录了他们之间合法的个数,如果大于max就更新。就这样一直遍历到i结束为止。最后输出max就是结果了
class Solution {
public:
int longestValidParentheses(string s)
{
if (s.size() < )
return ;
stack<int> sta;
int last = -, max = ;
for (int i = ; i < s.size(); i++)
{
if (s[i] == '(') // 如果是左括号,就将其对应的下标存到栈中
sta.push(i);
if (s[i] == ')') // 如果是右括号
{
if (sta.empty()) // 如果栈为空,last记录为i,也就是遍历到此i的最后一个不符合的右括号的位置
{
last = i;
}
else // 如果不空,那就pop一个左括号的下标
sta.pop();
}
if (sta.empty()) //如果以上操作,sta为空,那么就记录最后一个右括号到i的个数-1为合法的个数
max = std::max(max, i - last);
else // 如果sta还有左括号,那就用剩下的没有匹配的括号到i的个数-1为合法的个数和max比较
max = std::max(max, i - sta.top());
}
return max;
}
};
leetcode第31题--Longest Valid Parentheses的更多相关文章
- leetcode解题报告 32. Longest Valid Parentheses 用stack的解法
第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...
- LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]
题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- LeetCode (32) Longest Valid Parentheses
题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...
- 【LeetCode每天一题】Valid Parentheses(有效的括弧)
Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...
- leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解
dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...
- LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31
32. 最长有效括号 32. Longest Valid Parentheses 题目描述 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 每日一算法2019/6/ ...
- 【LeetCode每天一题】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 valid (wel ...
- [LeetCode] Longest Valid Parentheses 最长有效括号
Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...
随机推荐
- 几个cd快速提示
cd是project师每天都会用到的命令. 今天就来分享几条和cd有关的小技巧 cd 假设你用cd ~来进入当前用户的home文件夹的话,那么能够试试直接敲cd. 相同效果,少敲两下键盘. cd - ...
- atitit.基于组件的事件为基础的编程模型--服务器端控件(1)---------服务器端控件和标签之间的关系
atitit.基于组件的事件为基础的编程模型--服务器端控件(1)---------服务器端控件和标签之间的关系 1. server控件是要server了解了标签.种类型的server控件: 1 1. ...
- C++ 内部排序(一)
先讲两个概念,所谓内部排序,指待排序的节点均存储在内存中.所谓排序的稳定性,指排序后,值相等的两个元素原来相对的位置是否发生变化了.举个例子. 待排序列:3(1),1,5,3(2) 稳定排序:1,3 ...
- quick-cocos2d-x游戏开发【5】——创建菜单
一个菜单是游戏中的一个基本要素,quick在里面menuItem有两个包.一个是图片菜单.一个文本菜单. 一个.图片菜单ui.newImageMenuItem(params) 參数: image: 正 ...
- MsSqlServer 语句
--假设 成绩>100 优 --假设成绩>90 良 select * from TblScore select 英语成绩= (case when tEnglish>90 then ...
- unix您不能使用crontab设置运营计划
unix您不能使用crontab设置运营计划 在系统中进行crontab例如,设置在下列现象时有发生: 解决方法: 编辑cron文件内容: #EDITOR=vi #export EDITOR ...
- 第四章:Django 的模板系统(转)
在之前的章节中,你可能觉得例子中视图返回文本有点不妥.即是, HTML 是直接写在 Python 代码中的. 这种做法会导致这些问题: 要做任何设计上的更改就必须改写 Python 代 ...
- org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: sys.entity.Role; nested exception is org.hibernate.PersistentObjectException: 的解决方案
1.错误信息 org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist ...
- 《Javascript权威指南》十六学习笔记:BOM资源---BOM基本应用
BOM基本应用包括:管理浏览器历史记录.得到处理和解决浏览器的信息.本文介绍了这些应用程序. 一.浏览历史管理 1.history对象的方法和属性 History 对象包括用户(在浏览器窗体中)訪问过 ...
- Matrix (二维树状数组)
Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the ...