zoukankan      html  css  js  c++  java
  • 算法子串子序列最值问题

    leetcode 32.最长有效括号

    左神 中级提升班 3-2

    class Solution {
        public int f(String s) {
            //1
            char[] chs = s.toCharArray();
            int n = s.length();
            //表示以i结尾的最长有效子串
            int[] dp = new int[n];
            dp[0] = 0;
            //2
            for (int i = 1; i < n; i++) {
                //analyse chs[i]
                if (chs[i] == '(') {
                    dp[i] = 0;
                } else {
                    if (i - dp[i - 1] - 1 >= 0 && chs[i - dp[i - 1] - 1] == '(') {
                        dp[i] = dp[i - 1] + ((i - dp[i - 1] - 2 >= 0) ? dp[i - dp[i - 1] - 2] : 0) + 2;
                    }
                }
            }
            int ans = -1;
            for (int i = 0; i < n; i++) {
                ans = Math.max(ans, dp[i]);
            }
            return ans;
        }
    }
  • 相关阅读:
    052-34
    052-33
    052-32
    052-31
    052-30
    052-28
    react组件之间通信
    排序算法
    点外卖
    js的各种排序算法
  • 原文地址:https://www.cnblogs.com/t1314/p/15757722.html
Copyright © 2011-2022 走看看