Longest Valid Parentheses

原创 2015年11月19日 16:37:10

题目描述

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.

题目解答

解题思想

  • 遍历两遍, 第一遍把优先的’(’ 和 ‘)’ 转换成 ’ * ’ , 然后计算最长连续的*个数
  • 动态规划的思想

代码实现

public class Solution {
    public int longestValidParentheses(String s) {
       if(s == null || s.length() == 0)
            return 0;
        StringBuilder temp = new StringBuilder(s);
        ArrayDeque<Integer> stack = new ArrayDeque<>();
        for(int i = 0; i < s.length(); i++){
            if(s.charAt(i) == '(')
                stack.push(i);
            else if(!stack.isEmpty()){
                //注意
                temp.setCharAt(stack.pop(), '*');
                temp.setCharAt(i, '*');
            }
        }

        int longest = 0, part = 0;
        for(int i = 0; i < temp.length(); i++){
            if(temp.charAt(i) == '*'){
                part++;
            }else {
                longest = Math.max(longest, part);
                part = 0;
            }
        }

        //注意
        longest = Math.max(longest, part);
        return longest;
    }
}

DP

public class Solution {
    /**
     *
     1. 状态:
     DP[i]:以s[i-1]为结尾的longest valid parentheses substring的长度。

     2. 通项公式:
     s[i-1] = '(':
     DP[i] = 0

     s[i-1] = ')':找i前一个字符的最长括号串DP[i]的前一个字符j = i-2-DP[i-1]
     DP[i] = DP[i-1] + 2 + DP[j],如果j >=0,且s[j] = '('
     DP[i] = 0,如果j<0,或s[j] = ')'
     */
    public int longestValidParentheses(String s) {
       if(s == null || s.length() == 0 || s.length() == 1)
            return 0;


        int len = s.length();
        int[] dp = new int[len+1];
        Arrays.fill(dp, 0);

        int maxLen = 0;
        for(int i = 1; i < dp.length; i++){
            int j = i-2-dp[i-1];
            if(s.charAt(i-1) == '(' || j < 0 || s.charAt(j) == ')')
                dp[i] = 0;
            else {
                dp[i] = dp[i-1]+2+dp[j];
                maxLen = Math.max(dp[i], maxLen);
            }
        }

        return maxLen;
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode 32. Longest Valid Parentheses

问题描述 Given a string containing just the characters '(' and ')', find the length of the longe...

[leetcode]Longest Valid Parentheses

题目介绍: Given a string containing just the characters '(' and ')', find the length of the longe...
  • isssen
  • isssen
  • 2016年02月25日 14:52
  • 152

longest valid parentheses--LeetCode

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 (we...
  • doc_sgl
  • doc_sgl
  • 2013年10月03日 12:05
  • 3040

leetcode 032 Longest Valid Parentheses

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

LeetCode_32---Longest Valid Parentheses

Longest Valid Parentheses  Total Accepted: 36685 Total Submissions: 176120My Submissions Quest...

leetcode Longest Valid Parentheses

这个星期没有做题,跑出去玩了一圈。投个实习感觉被鄙视了,回来继续学习算了。 Given a string containing just the characters '(' and...

[LeetCode]32.Longest Valid Parentheses

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

[LeetCode] 最长合法括号 longest valid parentheses

给你一个字符串,该字符串仅仅包含 '(‘ 和 ')',找出最长的合法括号子串。例如:"(()" 的最长合法括号子串是 “()” ,长度是2。")()())" 的最长合法括号子串是 “()()” ,长度...

leetcode 32. Longest Valid Parentheses

leetcode 32. Longest Valid Parentheses 算法就是从左向右扫描,遇到左括号就入栈,遇到右括号有三种情况: 1.栈顶是左括号,那么用2取代那个左括号,这时如果栈顶下边...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Longest Valid Parentheses
举报原因:
原因补充:

(最多只允许输入30个字)