image.png
https://leetcode-cn.com/problems/longest-valid-parentheses/submissions/

我的解 - 滑动窗口 - 太慢

  1. class Solution {
  2. public int longestValidParentheses(String s) {
  3. int leftNum = 0;
  4. int rightNum = 0;
  5. int left = 0;
  6. int right = 0;
  7. int max = 0;
  8. Map<Integer,Integer> map = new HashMap<>();
  9. char[] cs = s.toCharArray();
  10. while(left < s.length() - 1){
  11. right = left;
  12. leftNum = 0;
  13. rightNum = 0;
  14. while(right < s.length()){
  15. if(cs[right] == ')'){
  16. if(leftNum <= rightNum){
  17. right++;
  18. while(right < s.length() && cs[right] == ')') right++;
  19. left = right - 1;
  20. break;
  21. }
  22. rightNum++;
  23. }else{
  24. leftNum++;
  25. }
  26. right++;
  27. if(leftNum == rightNum){
  28. max = Math.max(max, right-left);
  29. }
  30. }
  31. left++;
  32. }
  33. return max;
  34. }
  35. }

动态规划

  1. public class Solution {
  2. public int longestValidParentheses(String s) {
  3. int maxans = 0;
  4. int dp[] = new int[s.length()];
  5. for (int i = 1; i < s.length(); i++) {
  6. if (s.charAt(i) == ')') {
  7. if (s.charAt(i - 1) == '(') {
  8. dp[i] = (i >= 2 ? dp[i - 2] : 0) + 2;
  9. } else if (i - dp[i - 1] > 0 && s.charAt(i - dp[i - 1] - 1) == '(') {
  10. // 考虑一下这种情况 ()(()())
  11. dp[i] = dp[i - 1] + ((i - dp[i - 1]) >= 2 ? dp[i - dp[i - 1] - 2] : 0) + 2;
  12. }
  13. maxans = Math.max(maxans, dp[i]);
  14. }
  15. }
  16. return maxans;
  17. }
  18. }

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

  1. class Solution {
  2. public int longestValidParentheses(String s) {
  3. Stack<Integer> stack = new Stack<>();
  4. stack.push(-1);
  5. int max = 0;
  6. for(int i = 0;i < s.length(); i++){
  7. if(s.charAt(i) == '('){
  8. stack.push(i);
  9. }else{
  10. stack.pop();
  11. if(stack.empty()){
  12. stack.push(i);
  13. }else{
  14. max = Math.max(max, i - stack.peek());
  15. }
  16. }
  17. }
  18. return max;
  19. }
  20. }

递归 + 中心扩展

class Solution {
    // (right, left)
private Map<Integer, Integer> map = new HashMap<>();
// key: 右括号的索引, value: 对应左括号的索引

    public int longestValidParentheses(String s) {
        char[] arr = s.toCharArray();
        int max = 0;
        // 遍历中心
        for (int c = 0; c < arr.length - 1; c++) {
            if (arr[c] == '(' && arr[c + 1] == ')') {
                int r = expand(arr, c, c + 1);
                max = Math.max(max, r - map.get(r) + 1);
                c = r;
            }
        }
        return max;
    }

    // 中心扩展
    private int expand(char[] arr, int l, int r) {
        while (l >= 0 && r < arr.length && arr[l] == '(' && arr[r] == ')') {
            l--; r++;
        }
       //map.put(r - 1, l + 1);
        if (map.containsKey(l)) return expand(arr, map.get(l) - 1, r); // 继续递归扩展
        map.put(r - 1, l + 1); // (右, 左), ???这里为什么不在前面,测试时前后都不影响结果,到底为什么
        return r - 1;
    }

}