Java-stack

Stack: It is a type of linear table, and it is restricted to insert and delete operations at one end of the linear table. The end that allows insertion and deletion is called the top of the stack, and the end that does not allow insertion and deletion is called the bottom of the
Insert picture description here
stack . Although the stack has only two simple operations, push and pop, We can achieve more complex effects. For
example, check whether the parentheses in the string'{[()]}' can match the above.
Let’s talk about the idea: When {,[,( is matched, it is pushed onto the stack. Matched),],} is out Stack comparison
code implementation

import java.util.Scanner;  
import java.util.Stack;  
  
public class Main {
    
      
    public static void main(String[] args) {
    
      
        Scanner scan = new Scanner(System.in);  
        int N = scan.nextInt();  
        String s;  
        for (int i = 0; i < N; i++) {
    
      
            s = scan.next();  
            if (isMatch(s)) {
    
      
                System.out.println("Yes");  
            } else {
    
      
                System.out.println("No");  
            }  
        }  
    }  
    private static boolean isMatch(String s) {
    
      
        Stack<Character> sk = new Stack<Character>();  
        for (int i = 0; i < s.length(); i++) {
    
      
            if (s.charAt(i) == '(') {
    
      
                sk.push('(');  
            }  
            if (s.charAt(i) == ')') {
    
      
                if (!sk.isEmpty() && sk.pop() == '(')  
                    continue;  
                else  
                    return false;  
            }  
            if (s.charAt(i) == '[') {
    
      
                sk.push('[');  
            }  
            if (s.charAt(i) == ']') {
    
      
                if (!sk.isEmpty() && sk.pop() == '[')  
                    continue;  
                else  
                    return false;  
            }  
        }  
        if (sk.isEmpty())  
            return true;  
        else  
            return false;  
    }  
}  

And how does the computer calculate this formula?
1+(5+2)X6
We know that we should first calculate the brackets 5+2=7, then the multiplication number 7*6=42, and finally the plus sign 42+1=43.
How to use the stack for high-speed calculations? Draw up a few rules first

  1. All digital output directly
  2. Operator priority is higher than the push into the stack. Otherwise, pop all operators with higher or the same precedence from the stack or until the parentheses, and then push all the operators on the stack.
  3. All opening parentheses are pushed onto the stack
  4. If the left parenthesis is included in the stack, all operators are pushed onto the stack
  5. If it is a right parenthesis, the stack is popped continuously until the left parenthesis is matched

After these few rules, we get the suffix expression "152+6X+". We use the stack again to push any number on the stack, and any operator pops the stack twice,
so 152 is pushed into the stack and encounters the + sign, and 2 is popped out of the stack and then 5 is out. The stack is operated to get 7, and the result is pushed onto the stack. Then 6 is also pushed into the stack, and when it encounters the X number, it is popped twice, and 6 is popped out of the stack
and then 7 is popped out for calculation to get 42, and the result is pushed onto the stack. The last is the + sign, the stack is popped twice, 42 is popped first, and then the stack is popped for calculation. The stack is empty, and the final result is 43.

Guess you like

Origin blog.csdn.net/qq_36008278/article/details/115173545