LeetCode第32题:最长有效括号(困难)

LeetCode第32题:最长有效括号(困难)

  • 题目:给定一个只包含 ‘(’ 和 ‘)’ 的字符串,找出最长的包含有效括号的子串的长度。
  • 解法一:可以看得出来,我的这个解法特别不好。。。但还好是做出来了。
class Solution {
    public int longestValidParentheses(String s) {
        int len=s.length();
        int L=0;
        int R=L+1;
        int count=0;
        int len2=0;
        int lenmax=0;
        if(len < 2) return 0;
        while(R<len){
            while(L<len && s.charAt(L) == ')' ){
                if(len2 != 0) len2=0;
                L++;
            }
            count++;
            R=L+1;
            while(count != 0 && R<len){
                if(s.charAt(R) == '('){
                    R++;
                    count++;
                }else{
                    R++;
                    count--;
                }
            }
            if(count == 0){
                len2 += R-L;
                if(lenmax<len2)  lenmax=len2;
                L=R;
                R=L+1;
                //return L;
            }else{
                if(len2==0){
                    L++;
                    R=L+1;
                    count=0;
                }else{
                    L++;
                    R=L+1;
                    count=0;
                    len2=0;
                }    
            }
        }
        return lenmax;
    }
}

LeetCode第32题:最长有效括号(困难)_第1张图片

  • 解法二:官方的做法有很多,但是我挑了一个最好理解的。利用了栈的思想,遇到“(”就将角标记录下来,遇到“)”就把当前角标与最上面一个角标做差,计算出最长有效括号。
public class Solution {

    public int longestValidParentheses(String s) {
        int maxans = 0;
        Stack<Integer> stack = new Stack<>();
        stack.push(-1);
        for (int i = 0; i < s.length(); i++) {
            if (s.charAt(i) == '(') {
                stack.push(i);
            } else {
                stack.pop();
                if (stack.empty()) {
                    stack.push(i);
                } else {
                    maxans = Math.max(maxans, i - stack.peek());
                }
            }
        }
        return maxans;
    }
}

作者:LeetCode
链接:https://leetcode-cn.com/problems/longest-valid-parentheses/solution/zui-chang-you-xiao-gua-hao-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

 - 解法三:从后往前找“)”,然后利用递归思想
 

```java
public class Solution {
    public int longestValidParentheses(String s) {
        int maxans = 0;
        int dp[] = new int[s.length()];
        for (int i = 1; i < s.length(); i++) {
            if (s.charAt(i) == ')') {
                if (s.charAt(i - 1) == '(') {
                    dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2;
                } else if (i - dp[i - 1] > 0 && s.charAt(i - dp[i - 1] - 1) == '(') {
                    dp[i] = dp[i - 1] + ((i - dp[i - 1]) >= 2 ? dp[i - dp[i - 1] - 2] : 0) + 2;
                }
                maxans = Math.max(maxans, dp[i]);
            }
        }
        return maxans;
    }
}

作者:LeetCode
链接:https://leetcode-cn.com/problems/longest-valid-parentheses/solution/zui-chang-you-xiao-gua-hao-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

你可能感兴趣的:(学生)