LeetCode32. Longest Valid Parentheses

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()<2) return 0;

        Queue<String> queue = new LinkedList<String>();
        queue.offer(s);
        int max = Integer.MIN_VALUE;

        while(!queue.isEmpty()){
            String str = queue.poll();
            for(int i=0;i<str.length();i++){
                if(validParentheses(str)) return str.length();
                String str1 = str.substring(0,i)+str.substring(i+1);

                // System.out.println(str1.substring(0,i));
                // System.out.println(str1.substring(i));

                if(validParentheses(str1)){
                    System.out.println(str1);
                    System.out.println(validParentheses(str1));
                    return Math.max(str1.length(),max);
                }else{
                    queue.offer(str1);
                }
            }
        }
        return 0;
    }
    // /

    public boolean validParentheses(String s){

        int count = 0;

        for(int i=0;i<s.length();i++){
           if(s.charAt(i)=='(')  count++;
           if(s.charAt(i)==')'){
               if(count==0) return false;
               else{
                   count--;
               }
           }
        }
        return count==0;
    }
}

这里写了半天,其实是不符合题目要求的,题目要求的是找到一个substring,而我这里是remove掉一个符号,找一个最长的substring.

public class Solution {
    public int longestValidParentheses(String s) {
        if(s==null||s.length()<2) return 0;
        int max = Integer.MIN_VALUE;


        if(validParentheses(s)) return s.length();

          for(int i=0;i<s.length();i++){
                for(int j=i;j<=s.length();j++){
                    String str1 = s.substring(i,j);
                    if(validParentheses(str1)){
                        max = Math.max(str1.length(),max);
                    }
             }
         }

        return max;
    }
    // / 
    public boolean validParentheses(String s){       
        int count = 0;  
        for(int i=0;i<s.length();i++){
           if(s.charAt(i)=='(')  count++;
           if(s.charAt(i)==')'){
               if(count==0) return false;
               else{
                   count--;
               }
           }
        }
        return count==0;
    }
}

这里是brute force的方法,就是求出所有的substring parentheses。注意求substring的for循环里面,j是从i开始的

还有一种方法就是DP的方法,假设String的长度是s.length(). 假设longest[i]代表的是在 以i为结尾的substring的valid parenthese的长度,所以
if(s.charAt(i-1)==’(‘) we set longest[i] to zero.
if(s.charAt(i-1)==’)’)&&if(s.charAt(i-2)==’(‘) we set longest[i-2]+2;
如果s.charAt(i-2)等于’)’ s[i-longest[i-1]-1] == ‘(‘,
longest[i] = longest[i-1] + 2 + longest[i-longest[i-1]-2]
My solution uses DP. The main idea is as follows: I construct a array longest[], for any longest[i], it stores the longest length of valid parentheses which is end at i.

And the DP idea is :

If s[i] is ‘(‘, set longest[i] to 0,because any string end with ‘(’ cannot be a valid one.

Else if s[i] is ‘)’

 If s[i-1] is '(', longest[i] = longest[i-2] + 2

 Else if s[i-1] is ')' and s[i-longest[i-1]-1] == '(', longest[i] = longest[i-1] + 2 + longest[i-longest[i-1]-2]

For example, input “()(())”, at i = 5, longest array is [0,2,0,0,2,0], longest[5] = longest[4] + 2 + longest[1] = 6.

 int longestValidParentheses(string s) {
            if(s.length() <= 1) return 0;
            int curMax = 0;
            vector<int> longest(s.size(),0);
            for(int i=1; i < s.length(); i++){
                if(s[i] == ')'){
                    if(s[i-1] == '('){
                        longest[i] = (i-2) >= 0 ? (longest[i-2] + 2) : 2;
                        curMax = max(longest[i],curMax);
                    }
                    else{ // if s[i-1] == ')', combine the previous length.
                        if(i-longest[i-1]-1 >= 0 && s[i-longest[i-1]-1] == '('){
                            longest[i] = longest[i-1] + 2 + ((i-longest[i-1]-2 >= 0)?longest[i-longest[i-1]-2]:0);
                            curMax = max(longest[i],curMax);
                        }
                    }
                }
                //else if s[i] == '(', skip it, because longest[i] must be 0
            }
            return curMax;
        }


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值