算法题 20. 有效的括号

20. 有效的括号

1、自己思路

首先想到的就是使用栈,

class Solution {
    public boolean isValid(String s) {
        ArrayList<Character> c = new ArrayList<Character>();    //定义栈
        int c_top = -1;  //定义栈的指针
        
        
        for(int i=0; i<s.length(); i++){
            if(c_top == -1){    //当前数组为空
                c.add(0,s.charAt(i));
                c_top++;
                continue;
            }
            switch(s.charAt(i)){
                case ')':
                    if(c.get(c_top) == '('){
                        //出栈
                        c.remove(c_top);
                        c_top--;
                    }else{
                        //入栈
                        c_top++;
                        c.add(c_top, s.charAt(i));
                    }
                    break; 
                case ']':
                    if(c.get(c_top) == '['){
                        //出栈
                        c.remove(c_top);
                        c_top--;
                    }else{
                        //入栈
                        c_top++;
                        c.add(c_top, s.charAt(i));
                    }
                    break; 
                case '}':
                    if(c.get(c_top) == '{'){
                        //出栈
                        c.remove(c_top);
                        c_top--;
                    }else{
                        //入栈
                        c_top++;
                        c.add(c_top, s.charAt(i));
                    }
                    break; 
                default :
                     //入栈
                     c_top++;
                     c.add(c_top, s.charAt(i));
            }
           
        }
        
        if(c_top == -1){
            return true;
        }else{
            return false;
        }
    }
}

提交错误:

1、写反了判断符号,

2、没有写前符号入栈的操作

总结:

总的来说思路和官方的解答是一一致的。但是有几个问题需要注意:

1、关于判断的三种符号,我用的是switch进行判断,最好的办法应该是定义一个HashMap<Character,Character>

2、Java封装了Stack这个类

2、官方的代码,与一相同的思路

通过hashmap查找,找不到就是前符号,找到了就是后符号,直接到stack进行比较,判断。这个做法非常巧妙,比我那种每个都去比较最后只是去比是否栈为空要好。

决定重写。。。。

class Solution {
    public boolean isValid(String s) {
        //构建hashmap
        HashMap<Character, Character> hashmap = new HashMap<Character, Character>();
        hashmap.put(')','(');
        hashmap.put(']','[');
        hashmap.put('}','{');
        
        //构建栈,使用Java自带的stack
        Stack<Character> stack = new Stack<Character>();
        
        for(int i=0; i<s.length(); i++){
            //判断是否存在,就证明了这个字符是前字符还是后字符
            if(hashmap.containsKey(s.charAt(i))){
                //获取stack元素
                //这里要先判断stack是否为空,这里使用的是教程的代码,感觉处理的简介
                char topElement = stack.empty() ? '#' : stack.pop();
                
                if(topElement!= hashmap.get(s.charAt(i))){
                    return false;
                }
            }else{
                stack.push(s.charAt(i));
            }
        }
        
        return stack.isEmpty();
    }
}

提交错误:

1、topElement判断不熟悉

2、创建HashMap不熟悉

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章