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.

法I:把所有invalid的括号位置都标记出来,比较invalid之间的长度哪段最长

  1. class Solution {
  2. public:
  3. int longestValidParentheses(string s) {
  4. vector<int> invalidPos;
  5. invalidPos.push_back(-);
  6. invalidPos.push_back(s.length());
  7. stack<int> lParenPos;
  8. int len = , ret = ;
  9.  
  10. for(int i = ; i < s.length(); i++){
  11. if(s[i]=='('){
  12. lParenPos.push(i);
  13. }
  14. else{ //right parenthese
  15. if(lParenPos.empty()){
  16. invalidPos.push_back(i);
  17. }
  18. else{
  19. lParenPos.pop();
  20. }
  21. }
  22. }
  23.  
  24. while(!lParenPos.empty()){
  25. invalidPos.push_back(lParenPos.top());
  26. lParenPos.pop();
  27. }
  28.  
  29. sort(invalidPos.begin(), invalidPos.end());
  30. for(int i = ; i < invalidPos.size(); i++){
  31. len = invalidPos[i]-invalidPos[i-]-;
  32. if(len > ret) ret = len;
  33. }
  34.  
  35. return ret;
  36. }
  37. };

法II:动态规划

  1. class Solution {
  2. public:
  3. int longestValidParentheses(string s) {
  4. if(s.empty()) return ;
  5. stack<int> leftStack;
  6. int ret = ;
  7. int currentMax = ;
  8. int leftPos;
  9. vector<int> dp(s.length()+,); //currentMax无法检测到连续valid的情况,eg: ()(), 所以需要动态规划记录i位置之前连续多少个valid。
  10.  
  11. for(int i = ; i <s.length(); i++){
  12. if(s[i]==')'){
  13. if(leftStack.empty()){
  14. currentMax = ;
  15. }
  16. else
  17. {
  18. leftPos = leftStack.top();
  19. leftStack.pop();
  20. currentMax = i-leftPos+ + dp[leftPos];
  21. dp[i+] = currentMax;
  22. ret = max(ret,currentMax);
  23. }
  24. }
  25. else{
  26. leftStack.push(i); //push the index of '('
  27. }
  28. }
  29. return ret;
  30. }
  31. };

32. Longest Valid Parentheses (Stack; DP)的更多相关文章

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

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

  2. 32. Longest Valid Parentheses(最长括号匹配,hard)

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

  3. LeetCode 32 Longest Valid Parentheses(最长合法的括号组合)

    题目链接: https://leetcode.com/problems/longest-valid-parentheses/?tab=Description   Problem :已知字符串s,求出其 ...

  4. [Leetcode][Python]32: Longest Valid Parentheses

    # -*- coding: utf8 -*-'''__author__ = 'dabay.wang@gmail.com' 32: Longest Valid Parentheseshttps://oj ...

  5. 刷题32. Longest Valid Parentheses

    一.题目说明 题目是32. Longest Valid Parentheses,求最大匹配的括号长度.题目的难度是Hard 二.我的做题方法 简单理解了一下,用栈就可以实现.实际上是我考虑简单了,经过 ...

  6. leetcode 20. Valid Parentheses 、32. Longest Valid Parentheses 、

    20. Valid Parentheses 错误解法: "[])"就会报错,没考虑到出现')'.']'.'}'时,stack为空的情况,这种情况也无法匹配 class Soluti ...

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

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

  8. 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. 表格头部与左侧内容随滚动条位置改变而改变(基于jQuery)

    效果图如下: HTML代码: <!DOCTYPE html> <html lang="zh-CN"> <head> <meta chars ...

  2. Python 函数 memoryview()

    memoryview() 函数返回给定参数的内存查看对象(Momory view). 所谓内存查看对象,是指对支持缓冲区协议的数据进行包装,在不需要复制对象基础上允许Python代码访问.返回元组列表 ...

  3. bzoj 4407 于神之怒加强版——反演

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4407 \( ans = \sum\limits_{D=1}^{min(n,m)}\frac{ ...

  4. bzoj1040(ZJOI2008)骑士——基环树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1040 基环树的模板. 套路就是把环断开,先把一端作为根节点,强制不选:再把另一端作为根节点, ...

  5. MongoDB入门实践

    MongoDB入门实践 简单介绍MongoDB,包括MongoDB的使用场景.和MySQL的对比.安装部署.Java客户端访问及总结 MongoDB? 我们遵循需求驱动技术的原则,通过一个场景来引入M ...

  6. Unit07: MyBatis框架简介 、 MyBatis基本应用

    Unit07: MyBatis框架简介 . MyBatis基本应用 1. myBatis (1)myBatis是什么? 是一个开源的持久层框架. 注:myBatis底层仍然是jdbc. (2)编程步骤 ...

  7. Python Socke

    回射 SERVER #!/usr/bin/python3 #_*_ coding:utf- _*_ import socket,os,time import socketserver import t ...

  8. 贴一段demo代码,演示channel之间的同步

    package main import ( "fmt" "time" ) func deskGoRoutine(index int, userChannel c ...

  9. GOF23设计模式之观察者模式(observer)

    一.观察者模式概述 观察者模式主要用于 1 :N 的通知.当一个对象(目标对象 Subject 或 Observable)的状态变化时,它需要通知一系列对象(观察者对象 Observer),令它们做出 ...

  10. 在Windows下搭建基于nginx的视频直播和点播系统

    http://my.oschina.net/gaga/blog/478480 一.软件准备 由于nginx原生是为linux服务的,因此官方并没有编译好的windows版本可以下载,要在windows ...