20-有效的括号

可以利用replaceAll()函数实现目的,介绍可以看下面两个网址

https://www.runoob.com/java/java-string-replaceall.html

https://www.cnblogs.com/lzh1043060917/p/12791689.html

把完整的括号全部替换成“”,如果最后字符串长度为0,说明所有的括号都可以匹配

class Solution {
    public boolean isValid(String s) {
        while(s.contains("()") || s.contains("[]") || s.contains("{}")){
            s=s.replaceAll("\(\)","");
            s=s.replaceAll("\[\]","");
            s=s.replaceAll("\{\}","");
        }
        return s.length()==0;
    }
}

  

原文地址:https://www.cnblogs.com/lzh1043060917/p/12791699.html