Leikou 20 effective brackets (stack implementation)

1. Title

Insert picture description here

2. My initial thoughts and problems

1. Summarize some knowledge points before writing ideas:
(1) For String s, get the length
by s.length()method to
char c = s.charAt(i);get the character, i is the index starting from 0
(2) For the stack
creation:, Stack<Character> stack = new Stack<>();pay attention to the packaging class Character, for char
Push stack.push(c);
into the stack: pop out of the stack: stack.pop();
judge empty and judge the top element of the stack:
if(!stack.isEmpty() && stack.peek()== ‘(’){...}

2. Ideas
(1) The length of the string should be a double number, that is, s.length()%2 == 0, otherwise it will return false directly.
(2) When the character c is ([ {These three symbols, execute the stack push(c)
(3 ) When c is three kinds of right parentheses, discuss them separately. Check whether the stack is empty and whether peek() on the top of the stack is the corresponding left parenthesis. If so, pop the stack, otherwise return false.

3. Problems after running the code written by myself :
(1) Write c =='(' as double quotation marks. Note that c is a char type. Single quotation marks should be used, and double quotation marks should be used for strings. Pay attention to this, don’t pay attention. It is easy to overlook.
(2) After the code is written, it is forgotten return stack.isEmpty(), and it is found after self-inspection. After the traversal, the empty stack is executed, and the effective parenthesis is the empty stack.

3. Problem solving method one: stack

Directly on the code written by yourself:

class Solution {
    
    
    public boolean isValid(String s) {
    
    
        Stack<Character> stack = new Stack<>();
        //字符串包含的字符数能被2整除
        if(s.length() % 2 == 0){
    
    
            for(int i = 0;i < s.length();i++){
    
    
                char c = s.charAt(i);
                if(c == '(' || c== '[' || c == '{'){
    
    
                    //入栈
                stack.push(c);
                }else if(c == ')'){
    
    
                    if(!stack.isEmpty() && stack.peek() == '('){
    
    
                        stack.pop();
                    }else{
    
    
                        return false;
                    }
                }else if(c == ']'){
    
    
                    if(!stack.isEmpty() && stack.peek() == '['){
    
    
                        stack.pop();
                    }else{
    
    
                        return false;
                    }
                }else{
    
    
                    if(!stack.isEmpty() && stack.peek() == '{'){
    
    
                        stack.pop();
                    }else{
    
    
                        return false;
                    }
                }
                
            }
            //这一句不要忘记,一开始漏掉了
            return stack.isEmpty(); 
        }
        return false;
    }
}

The official problem solution uses HashMap in the stack. The problem solution is similar to the one written by myself, but the form of data storage is different. The official code is also put:

class Solution {
    
    
    public boolean isValid(String s) {
    
    
        int n = s.length();
        if (n % 2 == 1) {
    
    
            return false;
        }

        Map<Character, Character> pairs = new HashMap<Character, Character>() {
    
    {
    
    
            put(')', '(');
            put(']', '[');
            put('}', '{');
        }};
        Deque<Character> stack = new LinkedList<Character>();
        for (int i = 0; i < n; i++) {
    
    
            char ch = s.charAt(i);
            if (pairs.containsKey(ch)) {
    
    
                if (stack.isEmpty() || stack.peek() != pairs.get(ch)) {
    
    
                    return false;
                }
                stack.pop();
            } else {
    
    
                stack.push(ch);
            }
        }
        return stack.isEmpty();
    }
}

Guess you like

Origin blog.csdn.net/ambitionLlll/article/details/113943559