力扣:有效括号 & 最长有效括号

在这里插入图片描述


package com.likou;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

class ValidBrackets {

    class Case1 {
        public boolean isValid(String s) {
            ArrayList<String> list = new ArrayList<>(120);
            boolean a = true;
            for (int i = 0; i < s.length(); i++) {
                String s1 = s.substring(i, i + 1);
                if (getStr(s1) != null) {
                    list.add(getStr(s1));
                } else if (list.size() > 0 && list.get(list.size() - 1).equals(s1)) {
                    list.remove(list.size() - 1);
                } else {
                    a = false;
                    break;
                }
            }
            return a && list.size() == 0;
        }

        public String getStr(String s) {
            String a;
            switch (s) {
                case "(":
                    a = ")";
                    break;
                case "{":
                    a = "}";
                    break;
                case "[":
                    a = "]";
                    break;
                default:
                    a = null;
            }
            return a;
        }
    }

    class Case2 {
        public boolean isValid(String s) {
            Map<String, String> map = new HashMap<>();
            map.put("(", ")");
            map.put("[", "]");
            map.put("{", "}");
            ArrayList<String> list = new ArrayList<>(120);
            boolean a = true;
            for (int i = 0; i < s.length(); i++) {
                String s1 = s.substring(i, i + 1);
                if (map.get(s1) != null) {
                    list.add(map.get(s1));
                } else if (list.size() > 0 && list.get(list.size() - 1).equals(s1)) {
                    list.remove(list.size() - 1);
                } else {
                    a = false;
                    break;
                }
            }
            return a && list.size() == 0;
        }
    }

}

//给定一个只包括 '(',')','{','}','[',']' 的字符串 s ,判断字符串是否有效。
//
// 有效字符串需满足:
//
//
// 左括号必须用相同类型的右括号闭合。
// 左括号必须以正确的顺序闭合。
//
//
//
//
// 示例 1:
//
//
//输入:s = "()"
//输出:true
//
//
// 示例 2:
//
//
//输入:s = "()[]{}"
//输出:true
//
//
// 示例 3:
//
//
//输入:s = "(]"
//输出:false
//
//
// 示例 4:
//
//
//输入:s = "([)]"
//输出:false
//
//
// 示例 5:
//
//
//输入:s = "{[]}"
//输出:true
//
//
//
// 提示:
//
//
// 1 <= s.length <= 104
// s 仅由括号 '()[]{}' 组成
//
package com.example.demo;


import java.util.Stack;

class MaxValidBrackets {

    public static void main(String[] args) {
        String ss = "))))())(())()))(()()(())(())()))(((()()))()()))(())(())()())()(()())((()(((())()())(()())(())((()))))())()))()(())))())()))(()))((()())((()(())))(()))))))))((())(()()((())()()(()))))(((()(())))())))()())))())()()())()(())()(((())()))()()())))()())))()((((((())((())))((())())(((()())())()((((((()())((()()(())(()))(()())()))()(()(()())(()))((())((())))))()()))))()())()))))((((())(())))((()))(()()()()()((())((((())())()())()())(()(()()))())(((()())(()))()))(())()((()(())))))()())())()()(())))((())()()()))(())((()())))))((()((((()(((())()))))(()))()()))(()(())(()((()()()))))()))()()(((()()(())())()(())(()()()))()(()())))()((()((()))))())()(())()(()()((()()())(((())((())))(()())))()))()()())()))((()))(((()()()((()))))()()()))()))())())))))())()))(()))))(()()()))()((())))((())))()))(()()()()()()(()))())())(((())))(())(()(()())((()()()()))()()(()()))(()())(()()()((()()(((()(()((()((((()((())((()()))))))()())())(()(())()((((()()()()()))))()())()((())))))))()(((()())))()(()()(()()()()))()((((()((())(())))())))(()()()())()))))))((()))())((())(()(()(((()()()((((()()))())()())()())()))))())()(((()))))()()())))(())())))(())())((()))(())))(((()()))((((()))(()()))())((()())(()))(()(())(()(()))((((((()()(()()(()))()(()(())((((((((()(()())((())))())()())(()(()()))))(()(()()()))(()((()(((())((())(())()(()()())(()))())((()((((((())())(())()(()()(()())(())())()()))())(()))(())))()())))()()((()))())()()(())(()())()())())())))()()))((((()((()(())(((())()((())(())())))))()(()))())()))())(((())))))((((()(()()))))(((())(((())((())))))()()))()(()(((((((()))))((()))())()(())()))())())((()))))((())(())))(((())((((()(())(())()(((((())))()))()(()())(()()(())()(((()))())())))()))()()())((";
        long l = System.currentTimeMillis();
        int valid = isValid(ss);
        System.out.println(valid);
        System.out.println(System.currentTimeMillis() - l);
    }

    public static int isValid(String s) {
        if (s.length() < 2 || s.indexOf("(") < 0 || s.lastIndexOf(")") < 0) {
            return 0;
        }
        s = s.substring(s.indexOf("("), s.lastIndexOf(")") + 1);
        int a = 0;
        int aa = 0;
        Stack<String> stack = new Stack<>();
        Stack<String> stack1 = new Stack<>();
        int length = s.length();
        for (int i = 0; i < length; i++) {
            String s1 = s.substring(i, i + 1);
            String s2 = s.substring(length - i - 1, length - i);
            String s3 = get1(stack, s1);
            String s4 = get2(stack1, s2);

            if ("0".equals(s3) || "0".equals(s4)) {
                aa = a > aa ? a : aa;
                a = 0;
            } else {
                a++;
            }
        }
        aa = a > aa ? a : aa;
        return aa;
    }

    private static String get1(Stack<String> stack, String s1) {
        if (getStr(s1) != null) {
            stack.push(s1);
            return ".";
        } else if (stack.size() > 0) {
            stack.pop();
            return "1";
        } else {
            return "0";
        }
    }

    private static String get2(Stack<String> stack, String s1) {
        if (getStr1(s1) != null) {
            stack.push(s1);
            return ".";
        } else if (stack.size() > 0) {
            stack.pop();
            return "1";
        } else {
            return "0";
        }
    }

    public static String getStr(String s) {
        String a;
        switch (s) {
            case "(":
                a = ")";
                break;
            default:
                a = null;
        }
        return a;
    }

    public static String getStr1(String s) {
        String a;
        switch (s) {
            case ")":
                a = "(";
                break;
            default:
                a = null;
        }
        return a;
    }


}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值