zoukankan      html  css  js  c++  java
  • leetcode-32

    暴力法

    public class Solution {
        public boolean isValid(String s) {
            Stack<Character> stack = new Stack<Character>();
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '(') {
                    stack.push('(');
                } else if (!stack.empty() && stack.peek() == '(') {
                    stack.pop();
                } else {
                    return false;
                }
            }
            return stack.empty();
        }
        public int longestValidParentheses(String s) {
            int maxlen = 0;
            for (int i = 0; i < s.length(); i++) {
                for (int j = i + 2; j <= s.length(); j+=2) {
                    if (isValid(s.substring(i, j))) {
                        maxlen = Math.max(maxlen, j - i);
                    }
                }
            }
            return maxlen;
        }
    }

    暴力法也有可取之处,比如这处isValid就思路很明确

    题解有个动态规划,不需要这么麻烦啊关键是。

    栈实现

        public static 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;
        }

    对于括号匹配直接想到的基本都是栈,因为学数据结构的时候这就是一个栈的经典示例,但是此题还有一个更直接的方法,不需要额外的空间

    public class Solution {
        public int longestValidParentheses(String s) {
            int left = 0, right = 0, maxlength = 0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i) == '(') {
                    left++;
                } else {
                    right++;
                }
                if (left == right) {
                    maxlength = Math.max(maxlength, 2 * right);
                } else if (right >= left) {
                    left = right = 0;
                }
            }
            left = right = 0;
            for (int i = s.length() - 1; i >= 0; i--) {
                if (s.charAt(i) == '(') {
                    left++;
                } else {
                    right++;
                }
                if (left == right) {
                    maxlength = Math.max(maxlength, 2 * left);
                } else if (left >= right) {
                    left = right = 0;
                }
            }
            return maxlength;
        }
    }

    这个思路就是左到右,右到左直接的遍历处理,当不满足的时候置零是此思路的关键。

    一个没有高级趣味的人。 email:hushui502@gmail.com
  • 相关阅读:
    Java设计模式之单例模式
    Bootstrap-table使用footerFormatter做统计列
    Bootstrap进度条
    基于Bootstrap的表格插件bootstrap-table
    基于Bootstrap的对话框插件bootstrap-dialog
    基于Bootstrap的下拉框插件bootstrap-select
    JsonConfig处理日期时间
    Jquery表单验证插件validate
    Hibernate使用Criteria去重distinct+分页
    设置iframe高度自适应屏幕高度
  • 原文地址:https://www.cnblogs.com/CherryTab/p/12194392.html
Copyright © 2011-2022 走看看