Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring.

Example 1:

  1. Input: "(()"
  2. Output: 2
  3. Explanation: The longest valid parentheses substring is "()"

Example 2:

  1. Input: ")()())"
  2. Output: 4
  3. Explanation: The longest valid parentheses substring is "()()"
  1. class Solution {
  2. public int longestValidParentheses(String s) {
  3. int[] dp = new int[s.length()]; //longest valid parentheses end with current index
  4. Stack<Integer> stack = new Stack<>(); //index of each left parenthese
  5. int ret = 0;
  6.  
  7. for(int i = 0; i < s.length(); i++){
  8. if(s.charAt(i) == '('){
  9. stack.push(i);
  10. }
  11. else if(!stack.empty()){ //')' and have left parenthese in the stack
  12. if(stack.peek() > 0)
  13. dp[i] = dp[stack.peek()-1] + (i-stack.peek()+1);
  14. else //first index
  15. dp[i] = i-stack.peek()+1;
  16. stack.pop();
  17. }
  18. else{ //')' and have no left parenthese in the stack
  19. stack.clear();
  20. }
  21. }
  22.  
  23. for(int i = 0; i < s.length(); i++){
  24. if(dp[i]>ret) ret = dp[i];
  25. }
  26. return ret;
  27. }
  28. }

最长有效括号不仅与当前stack的情况有关,也与有效做括号之前的stack情况有关,所以用动态规划。

使用一维动态规划记录以当前位置结束的最长有效括号。

32. Longest Valid Parentheses (JAVA)的更多相关文章

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

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

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

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

  3. 刷题32. Longest Valid Parentheses

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

  4. Java [leetcode 32]Longest Valid Parentheses

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

  5. 32. Longest Valid Parentheses

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

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

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

  7. leetcode 32. Longest Valid Parentheses

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

  8. leetcode problem 32 -- Longest Valid Parentheses

    Longest Valid Parentheses Given a string containing just the characters '(' and ')', find the length ...

  9. 【Python】32. Longest Valid Parentheses

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

随机推荐

  1. pycharm 安装激活

    下载pycharm :http://www.jetbrains.com/pycharm/download/download 安装 直到 finish 下载补丁jetbrains-agent.jar并添 ...

  2. CodeChef---- February Challenge 2018----Points Inside A Polygon

    链接:https://www.codechef.com/FEB18/problems/POINPOLY Points Inside A Polygon Problem Code: POINPOLY Y ...

  3. hive里面union all的用法记录

    UNION用于联合多个select语句的结果集,合并为一个独立的结果集,结果集去重. UNION ALL也是用于联合多个select语句的结果集.但是不能消除重复行.现在hive只支持UNION AL ...

  4. Vue左滑组件slider的实现

    本文链接:https://blog.csdn.net/latency_cheng/article/details/82983000 slider组件与swiper组件不同,slider滑动时并不翻页, ...

  5. @清晰掉 swap函数

    swap函数估计是一个各种各样程序都会频繁用到的子程序,可是你知道它究竟有多少种不同的写法吗?下面我就列举我知道的几种swap函数来跟大家分享一下. (1)经典型---嫁衣法 无论是写程序还是干其他事 ...

  6. windows程序调试

    由于不能在控制台输出,可以使用Messagebox 但是有时候要用到输出int之类的,需要转换.转换过程中有会有很多问题. 这里给出两个可行的代码 int a = 5, b = 10; int res ...

  7. FTP协议的两种工作模式简单解析!

    转载自百度百科:http://baike.baidu.com/link?url=KaBZmDM4IZ2v56MyoOnpjqKr0gADv_BRbgjlscYdyvh3-zDwINOHNPSi9Jlp ...

  8. 阶段3 1.Mybatis_12.Mybatis注解开发_4 mybatis注解开发CRUD的其他操作

    delete 51已经被删除掉了. 查询一个 findUserByName模糊查询 带百分号的情况 value这个参数是固定的 返回值为int类型的

  9. C# 导出Excel文件 所导出文件打开时提示“Excel文件格式与扩展名指定格式不一致”

    Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application(); ...

  10. oracle 普通数据文件备份与恢复

    普通数据文件指:非system表空间.undo_tablespace表空间.临时表空间和只读表空间的数据文件.它们损坏导致用户数据不能访问,不会导致db自身异常.实例崩溃.数据库不恢复就无法启动的情况 ...