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的更多相关文章

  1. leetcode解题报告 32. Longest Valid Parentheses 用stack的解法

    第一道被我AC的hard题!菜鸡难免激动一下,不要鄙视.. Given a string containing just the characters '(' and ')', find the le ...

  2. LeetCode 笔记系列八 Longest Valid Parentheses [lich你又想多了]

    题目:Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  3. LeetCode (32) Longest Valid Parentheses

    题目 Given a string containing just the characters '(' and ')', find the length of the longest valid ( ...

  4. 【LeetCode每天一题】Valid Parentheses(有效的括弧)

    Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the inpu ...

  5. leetcode解题报告 32. Longest Valid Parentheses 动态规划DP解

    dp[i]表示以s[i]结尾的完全匹配的最大字符串的长度. dp[] = ; ; 开始递推 s[i] = ')' 的情况 先想到了两种情况: 1.s[i-1] = '(' 相邻匹配 这种情况下,dp ...

  6. LeetCode 32. 最长有效括号(Longest Valid Parentheses) 31

    32. 最长有效括号 32. Longest Valid Parentheses 题目描述 给定一个只包含 '(' 和 ')' 的字符串,找出最长的包含有效括号的子串的长度. 每日一算法2019/6/ ...

  7. 【LeetCode每天一题】Longest Valid Parentheses(最长有效括弧)

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  8. 【leetcode刷题笔记】Longest Valid Parentheses

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

  9. [LeetCode] Longest Valid Parentheses 最长有效括号

    Given a string containing just the characters '(' and ')', find the length of the longest valid (wel ...

随机推荐

  1. Oracle经常使用函数

    Oracle经常使用函数 --TRUNC,TO_DATE,TO_CHAR,TO_NUMBER, SUBSTR,REPLACE.NVL .TRIM,wm_concat,upper, lower,leng ...

  2. UNIX网络编程卷1 server编程范式0 迭代server

    本文senlie原版的.转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.迭代 TCP server总是在全然处理某个客户的请求后才转向下一个客户. 2.从进程控 ...

  3. hdu 1233(还是畅通project)(prime算法,克鲁斯卡尔算法)(并查集,最小生成树)

    还是畅通project Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Tota ...

  4. JNDI数据源配置注意事项

    假设是在原有project上改动 1,applicationContext.xml中改动当中的value值 <bean id="dataSource" class=" ...

  5. 可视化配置以及Net应用MemCache在win7

    MemCache在win7上的可视化配置以及Net应用   惯例科普:MemCache是一套分布式的高速缓存系统,由LiveJournal的Brad Fitzpatrick开发,但目前被许多网站使用以 ...

  6. ORACLE在表中添加的目光凝视和读取列

    在ORACLE中给表.列添加凝视以及读取凝视 1.给表填加凝视:SQL>comment on table 表名 is '表凝视"; 2.给列加凝视:SQL>comment on ...

  7. FPGA 设计流程,延迟,时间

    FPGA 设计流程,延迟,时间 流程:每个时钟周期可以传输的数据比特. 延迟:从输入到时钟周期的输出数据需要经验. 时间:两个元件之间的最大延迟,最高时钟速度. 1 採用流水线能够提高 流量: 比如计 ...

  8. Android JNI环境要SQLite加密模块简介

    免费的SQLite开源源代码仅仅给提供了两个函数,仅仅有实现这两个函数才干实现数据库总体加密. 然后废了点劲从网上找了一个已经实现好的开源库http://sourceforge.net/project ...

  9. Layout Renderers

    Layout Renderers NLog package ${activityid} - Puts into log a System.Diagnostics trace correlation i ...

  10. tableView 短剪线离开15像素问题

    ios7于,UITableViewCell左将默认15空白像素. 建立setSeparatorInset:UIEdgeInsetsZero 空白可以去除. ios8中.setSeparatorInse ...