【Longest Valid Parentheses】cpp
题目:
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.
代码:
class Solution {
public:
int longestValidParentheses(string s) {
int global_longest = ;
int last_not_match = -;
stack<int> sta;
for ( int i = ; i < s.length(); ++i )
{
if ( s[i]=='(')
{
sta.push(i);
continue;
}
if ( s[i]==')')
{
if ( sta.empty() )
{
last_not_match = i;
}
else
{
sta.pop();
if ( sta.empty() )
{
global_longest = std::max( global_longest, i-last_not_match );
}
else
{
global_longest = std::max( global_longest, i-sta.top() );
}
}
}
}
return global_longest;
}
};
tips:
这里巧用堆栈,核心是堆栈里存放的不是字符本身而是字符所在的索引值。
额外再用一个变量保存上一次没有匹配的字符的索引值。
每次更新global_longest的时候,都判断堆栈是否清空:
1. 如果堆栈清空了,则证明直到last_not_match之前的都是有效的字符串,所以完整有效长度为i-last_not_match
2. 如果堆栈没有清空,则说明还有‘(’没有被匹配上,所以临时有效长度i-sta.top()
这里设定last_not_match初始值为-1,主要是考虑如果从第一个元素就包含在有效范围内的test case (如,“()”)
=======================================
此题还有DP的解法,但总感觉DP解法思路不太直观,怪怪的。
留一个看过的DP解法的blog,以后有时间再研究
http://bangbingsyb.blogspot.sg/2014/11/leetcode-longest-valid-parentheses.html
=========================================
第二次过这道题,一开始写了一个动态规划的算法:逻辑可能是对的,但是绝对超时。
还是强化记忆了一下stack的做法:巧用栈的特点,O(n)复杂度搞定。
class Solution {
public:
int longestValidParentheses(string s) {
stack<int> sta;
int ret = ;
int last_not_match = -;
int i=;
while ( i<s.size() )
{
if ( s[i]=='(' )
{
sta.push(i);
}
else
{
if ( sta.empty() )
{
last_not_match = i;
}
else
{
sta.pop();
if (sta.empty())
{
ret = max(ret,i - last_not_match);
}
else
{
ret = max(ret, i - sta.top());
}
}
}
++i;
}
return ret;
}
};
【Longest Valid Parentheses】cpp的更多相关文章
- 【Valid Parentheses】cpp
题目: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the ...
- 【Longest Common Prefix】cpp
题目: Write a function to find the longest common prefix string amongst an array of strings. 代码: class ...
- 【Longest Palindromic Substring】cpp
题目: Given a string S, find the longest palindromic substring in S. You may assume that the maximum l ...
- 【Longest Consecutive Sequence】cpp
题目: Given an unsorted array of integers, find the length of the longest consecutive elements sequenc ...
- 【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 ...
- 【LeetCode练习题】Longest Valid Parentheses
Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...
- 【Python】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
一天一道LeetCode系列 (一)题目 Given a string containing just the characters '(' and ')', find the length of t ...
随机推荐
- JavaScript高级 面向对象的程序设计 (一)《JavaScript高级程序设计(第三版)》
创建对象 继承 面向对象的语言都有一个表示---类.通过类我们可以创建多个具有相同属性的对象.但是,在JS中并没有类的概念,所以JS的对象也和其他语言的对象不同. 对象的定义:无序的属性集合,其属性可 ...
- [视频]ARM告诉你物联网怎么玩,mbed 6LoWPan demo
该视频演示了基于arm mbed的物联网设备间的6LoWPAN应用,如连接家里的土壤湿度传感器,灯光控制,安防联动等应用. 演示视频 原创文章,转载请注明: 转载自 http://www. ...
- 将ubuntu12.04中,gcc4.6/g++4.6版本降低到gcc4.4/g++4.4.
降低Ubuntu中gcc和g++的版本 ubuntu 12.04 中带的gcc/g++都是4.6,将其降到4.4. 操作步骤如下: 一.降低gcc版本 1. $sudo apt-get install ...
- 一个简单且丑陋的js切换背景图片基础示例
不多说,直接上代码,非常基础的一个原生js切换元素背景图片范例 <html> <head> <meta http-equiv="Content-Type&quo ...
- Data Being Added Conflicts with Existing Data
While developing a page with multiple scrolls levels, and especially when using a grid, you may get ...
- SQL Server编程(05)游标
在关系数据库中,我们对于查询的思考是面向集合的.而游标打破了这一规则,游标使得我们思考方式变为逐行进行.对于类C的开发人员来着,这样的思考方式会更加舒服. 正常面向集合的思维方式是: 而对于游标来说: ...
- LotusPhp入口文件解析
LotusPhp也是单入口的框架,可以根据需要开启多个应用实例 例如前台页面可以用index.php作为入口文件,后台可以用admin.php作为入口文件,多个应用实例可以共享应用配置和类库或者根本每 ...
- recurse_array_change_key_case()递规返回字符串键名全为小写或大写的数组
//递归返回字符串键名全为小写或大写的数组function recurse_array_change_key_case(&$input, $case = CASE_LOWER){ if( ...
- linux资源监控命令详解
Linux统计/监控工具SAR详细介绍:要判断一个系统瓶颈问题,有时需要几个 sar 命令选项结合起来使用,例如: 怀疑CPU存在瓶颈,可用 sar -u 和 sar -q deng 等来查看 怀疑内 ...
- 画画板--第三方开源--DrawableView
Android上的第三方开源DrawableView支持手写,类似于写字板.DrawableView支持改变画笔颜色,画笔线条粗细,画布的手势缩放和拖曳显示部分区域.并最终支持将手绘的图保存到本地.在 ...