有效括号

有效括号

public class tt {
    int b;
    public boolean val(String s) {
        ArrayList<Integer> list=new ArrayList<>();
        if(getke(s.charAt(0))<0 || s.length()%2!=0){
            return false;
        }
        for (int i=0;i<s.length();i++){
             int b=getke(s.charAt(i));
             if(b>0){
                 list.add(b);
                 }
             else {
                 if(list.size()==0){
                     return false;
                 }
                 if(getke(s.charAt(i))+ list.get(list.size()-1)==0){
                     list.remove(list.size()-1);
                     System.out.println(list.size());
                 }
                 else{
                     return false;
                 }
             }
        }

        if(list.size()==0){
            return true;
        }
        else {
            return false;
        }
    }
    public int getke(char ch){
        switch (ch){
            case '(':return 1;
            case ')':return -1;
            case '[':return 2;
            case ']':return -2;
            case '{':return 3;
            case '}':return -3;
            default:return 0;
        }
    }
}
posted @   我是一个大废物  阅读(28)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示