LeetCode 150: Reverse Polish expression evaluation Evaluate Reverse Polish Notation

topic:

According to Reverse Polish Notation , evaluate the expression.

Effective operators include +, -, *, /. Each operand can be an integer, it can be another reverse Polish notation.

Evaluate the value of an arithmetic expression in Reverse Polish Notation.

Valid operators are +, -, *, /. Each operand may be an integer or another expression.

Description:

  • Leaving only the integer portion integer division.
  • Given reverse Polish notation always effective. In other words, the effective numerical expression is always obtained and there is no divisor is zero.

Note:

  • Division between two integers should truncate toward zero.
  • The given RPN expression is always valid. That means the expression would always evaluate to a result and there won't be any divide by zero operation.

Example 1:

输入: ["2", "1", "+", "3", "*"]
输出: 9
解释: ((2 + 1) * 3) = 9

Example 2:

输入: ["4", "13", "5", "/", "+"]
输出: 6
解释: (4 + (13 / 5)) = 6

Example 3:

输入: ["10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"]
输出: 22
解释: 
  ((10 * (6 / ((9 + 3) * -11))) + 17) + 5
= ((10 * (6 / (12 * -11))) + 17) + 5
= ((10 * (6 / -132)) + 17) + 5
= ((10 * 0) + 17) + 5
= (0 + 17) + 5
= 17 + 5
= 22

Extended:

Reverse Polish Notation , its grammar provision, the expression must give way to reverse Polish notation. Reverse Polish Notation also called postfix notation. This knowledge in data structures and compiler theory in these two courses are introduced, the following are some examples:

a+b ---> a,b,+
a+(b-c) ---> a,b,c,-,+
a+(b-c)*d ---> a,b,c,-,d,*,+
a+d*(b-c)--->a,d,b,c,-,*,+
a=1+3 ---> a,1,3,+,=

As can be seen from the above example:

(1) In both expressed in the same order as the operands appear;

(2) in the suffix, the operator according to the actual calculation order from left to right, and each operator always follows after its operands.

This expression is against humanity, but very friendly to your computer because the computer operation is the use of stack data structure.

Problem-solving ideas:

As can be seen in reverse Polish notation operators belonging to each of the operation between the two numbers before the operator. Such as:

如波兰表达式:1,2,+
则加号前两个数字为1,2。其运算符就是加号:1+2
得出结果:1+2=3

如波兰表达式:1,2,3,+,-
则加号前两个数字为2,3。其运算符就是加号:2+3
得出结果2+3=5,则波兰表达式变为:1,5,-
减号前两个数字为1,5,其运算符就是减号:1-5
得出结果1-5=-4

Examples of the above idea is very clear, direct pointer traversal expression, a numeral on the stack, the operator will encounter pop two numbers, the outcome after their operation, then the stack as a separate number. The last element in the stack that is only a result of the operation expression. It can also be used recursively

Java:

class Solution {
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (String t : tokens) {
            if (t.equals("+")) {
                stack.push(stack.pop() + stack.pop());
            } else if (t.equals("-")) {
                int tmp = stack.pop();
                stack.push(stack.pop() - tmp);
            } else if (t.equals("*")) {
                int tmp = stack.pop();
                stack.push(stack.pop() * tmp);
            } else if (t.equals("/")) {
                int tmp = stack.pop();
                stack.push(stack.pop() / tmp);
            } else {
                stack.push(Integer.parseInt(t));
            }
        }
        return stack.pop();
    }
}

Python:

python may be used in place of the stack array to answer this question the above methods. Here, instead of the four judgment if another function eval ():

class Solution:
    def evalRPN(self, tokens: List[str]) -> int:
        stack = []
        for t in tokens:
            if t in '+-*/':
                tmp = stack.pop()
                stack.append(int(eval('stack.pop()' + t + 'tmp')))
            else:
                stack.append(int(t))
        return stack.pop()

eval () function can perform a string parameter passed in the statement.

As eval('print("hhhhh")')will print out the execution parameter string hhhhh

Welcome to the concern micro-channel public number: Write Love Bug
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/zhangzhe532/p/11303284.html