[One question per day] Day6 Reverse Polish expression evaluation

This question comes from Leekow, the link is as follows: [Programming question] Reverse Polish expression evaluation

Day6 Reverse Polish Expression Evaluation

1. Topic requirements

Topic display:

Evaluates the expression in reverse Polish notation.

Valid operators include +, -, *, /. Each operand can be an integer or another reverse Polish expression.

Note that division between two integers keeps only the integer part.

It is guaranteed that a given reverse Polish expression will always be valid. In other words, the expression will always result in a valid value and there will be no division by zero.

tip:
Reverse Polish expression:

Reverse Polish expression is a kind of suffix expression, the so-called suffix is ​​that the indicator is written after.

The usual calculation formula is a kind of infix expression, such as ( 1 + 2 ) * ( 3 + 4 ).
The reverse Polish expression of this formula is written as ( ( 1 2 + ) ( 3 4 + ) * ).
Reverse Polish expressions mainly have the following two advantages:

There is no ambiguity in the expression after removing the parentheses, and the correct result can be calculated according to the sequence 1 2 + 3 4 + *even .

It is suitable for stack operations: when a number is encountered, it is pushed onto the stack; when an operator is encountered, the two numbers on the top of the stack are taken out for calculation, and the result is pushed onto the stack.


Enter a description:

Enter the expression, each item of tokens.

Output description:

Outputs the value converted to the result of an infix arithmetic expression.

Example 1:

input: tokens = ["2","1","+","3","*"]
output:9

Explanation: This formula is transformed into a common infix arithmetic expression: ((2 + 1) * 3) = 9

Example 2:

input: tokens = ["4","13","5","/","+"]
output:6

Explanation: This formula is transformed into a common infix arithmetic expression: (4 + (13 / 5)) = 6


2. Problem-solving ideas

[Problem Analysis]:

This question is a question on Likou. Simply put, the suffix expression in the array is converted back to the infix expression, and then the value of the infix expression is calculated.

First of all, we need to understand what is a postfix expression and what is an infix expression. Infix expression (or infix notation) is a general representation of arithmetic or logical formulas, that is, what we usually see is infix expressions, such as (3+4)*5this. The suffix expression is that + - * /these symbols are placed behind the numbers.

Here is a simple infix-to-suffix method:

[Problem solving ideas]:

We use stacks to do this problem, and the idea is very simple. The idea of ​​a stack is a box, which is put in first and then taken out, so we can put postfix expressions in one by one, and then we know that an operator is preceded by a number, so when we encounter an operator, we take it out to operate That's it, then put the result back and repeat.

For example, (3+4)*5-2, the postfix expression is 34+5*2-.

We first create a stack, then put it in 3, then put it in 4, and then put it in again +. At this time, it is an operator, so we take it out, then take out 4, and then 3perform the operation to get it 7, put it in 7, and then take it out 5. Take *, is an operator, take out 5, take out again 7, and perform operations to get 35...

So our steps are:

1. Create a stack
2. Take out the elements of the suffix expression one by one and put them into the stack
3. Before putting them in
, judge whether they are operators Carry out the corresponding operation
5. Return the result


3. Reference code

class Solution {
    
    
    public int evalRPN(String[] tokens) {
    
    
           
            Stack<Integer> stack = new Stack<>();//创建栈
            for(int i=0;i<tokens.length;i++){
    
    
                String val = tokens[i];
                if(func(val) == false){
    
    
                    stack.push(Integer.parseInt(val));
                }else{
    
    
                    int num2 = stack.pop();
                    int num1 = stack.pop();  
                switch(val){
    
    
                        case "+":
                            stack.push(num1+num2);
                            break;
                        case "-":
                            stack.push(num1-num2);
                            break;
                        case "*":
                            stack.push(num1*num2);
                            break;
                        case "/":
                            stack.push(num1/num2);
                            break;
                    }
                }
            }
             return stack.pop();
    }

    //判断是否是+ - * /的方法
    private boolean func(String str){
    
    
        if(str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/") ){
    
    
            return true;
        }
        return false;
    }
}

Guess you like

Origin blog.csdn.net/weixin_54225715/article/details/124578451