Leetcode 32. 最长有效括号

题目

给定一个只包含 ‘(’ 和 ‘)’ 的字符串,找出最长的包含有效括号的子串的长度。

示例 1:

输入: “(()”
输出: 2
解释: 最长有效括号子串为 “()”

示例 2:

输入: “)()())”
输出: 4
解释: 最长有效括号子串为 “()()”

解答

解法一:暴力解法

暴力解法思路很简单:对每一个子串都会判断括号是否有效匹配。

复杂度:O(n ^ 2) 的时间,O(n) 的空间。

这个算法显然不怎么样, Leetcode 不让通过,TLE 。。

代码
class Solution {
    public int longestValidParentheses(String s) {
        int res = 0;
        char[] charArray = s.toCharArray();        
        for(int i = 0; i < s.length(); i ++) {
            for(int j = i + 2; j <= s.length(); j += 2) {
                if(isValid(charArray, i, j)) {
                   res = Math.max(res, j - i); 
                }
            }
        }
        
        return res;
    }
    
    private boolean isValid(char[] charArray, int start, int end) {
        LinkedList<Character> stack = new LinkedList<>();
        for(int i = start; i < end; i ++) {
            char ch = charArray[i];
            if(ch == '(') {
                stack.push(ch);
            } else {
                Character top = stack.peek();
                if(top == null || top != '(') return false;
                stack.pop();
            }
        }
        
        return stack.isEmpty();
    }
}
结果

在这里插入图片描述

解法二:栈内隔板

向栈中压入 每个字符在字符串内的索引位置

注意:每个栈中的右括号被视为了一个隔板,它指明了前一个连续有效匹配了括号的子串的 终止点

只要每次遇见隔板时都计算一下最大值就好了。

复杂度:O(n) 的时间, O(n) 的空间。

代码
class Solution {
    public int longestValidParentheses(String s) {
        int res = 0;
        LinkedList<Integer> stack = new LinkedList<>();
        stack.push(-1);
        for(int i = 0; i < s.length(); i ++) {
            char ch = s.charAt(i);
            if(ch == '(') {
                stack.push(i);
            } else {
                stack.pop();
                
                if(stack.isEmpty()) {
                    stack.push(i);
                } else {
                    res = Math.max(res, i - stack.peek());
                }
            }
            
        }
        
        return res;
    }
}
结果

在这里插入图片描述

解法三:动态规划

使用 dp[i] 表示:以 i 结尾的最长有效括号

有以下几种情况:

  1. 当 s[i] 为 ‘ ( ’ :

    1. dp[i] 一定为 0 ,因为不可能组成有效的括号。
  2. 当 s[i] 为 ‘ ) ’ :

    1. 当 s[i-1] 为 ‘ ( ’ ,dp[i] = dp[i-2] + 2

    2. 当 s[i-1] 为 ‘ ) ’,且 s[i-dp[i-1] - 1] 为 ‘ ( ’,dp[i] = dp[i-1] + dp[i-dp[i-1]-2] + 2

注意:控制好边界的问题。

复杂度:O(n) 的时间, O(n) 的空间。

代码
class Solution {
    public int longestValidParentheses(String s) {
        if(s == null || s.length() == 0) return 0;
        
        int[] dp = new int[s.length()];
        int res = 0;
        for(int i = 1; i < s.length(); i ++) {
            char ch = s.charAt(i);
            if(ch == '(') {
                dp[i] = 0;
            } else {
                if(s.charAt(i - 1) == '(') {
                    dp[i] = (i - 2 >= 0 ? dp[i - 2] : 0) + 2;
                } else if(i - dp[i - 1] - 1 >= 0 && s.charAt(i - dp[i - 1] - 1) == '(') {
                    dp[i] = dp[i - 1] + (i - dp[i - 1] - 2 >= 0 ? dp[i - dp[i - 1] - 2] : 0) + 2;
                }
                
                res = Math.max(res, dp[i]);
            }
        }
        
        return res;
    }
}
结果

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值