Leetcode: Evaluate Reverse Polish Notation, leetcodenotation
Evaluate the value of an arithmetic expression in Reverse Polish Notation.Valid operators are +, -, *, /. Each operand may be an integer or another expression.Some examples: ["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
Difficulty: 70. The Chinese RPN name is called the inverse Polish notation. The advantage of RPN is that it does not need parentheses to represent the order of operations. It can be solved directly by the formula itself. The solution is to maintain a stack. When a number is used, the stack is written into the stack. When an operator is used to exit the stack twice, the two operands at the top of the stack are obtained. After the operator is used for calculation, the result is written into the stack. Until the sub-statement ends, the only element in the stack is the result.
The above Code does not handle errors in the case of reverse-Polish errors. In fact, it is not difficult, that is, every pop operation checks the empty stack. If the stack is empty, throw an exception. In addition, check the stack size. If it is not 1, it indicates that the number of operations is too large and an error is returned.
1 public class Solution { 2 public int evalRPN(String[] tokens) { 3 if (tokens==null || tokens.length==0) return 0; 4 LinkedList<Integer> stack = new LinkedList<Integer>(); 5 for (int i=0; i<tokens.length; i++) { 6 if (tokens[i].equals("+") || tokens[i].equals("-") || tokens[i].equals("*") || tokens[i].equals("/")) { 7 if (stack.isEmpty()) return 0; 8 int operand1 = stack.pop(); 9 if (stack.isEmpty()) return 0;10 int operand2 = stack.pop();11 if (tokens[i].equals("+")) stack.push(operand2 + operand1);12 if (tokens[i].equals("-")) stack.push(operand2 - operand1);13 if (tokens[i].equals("*")) stack.push(operand2 * operand1);14 if (tokens[i].equals("/")) stack.push(operand2 / operand1);15 }16 else {17 stack.push(Integer.parseInt(tokens[i]));18 }19 }20 return stack.peek();21 }22 }
What is rpn?
Rpn
RPN
Bilingual comparison
Dictionary results:
Rpn
[English] ['principal: rp 'I: 'en] [us] ['principal: rp' I: 'en]
Abbr. reverse polish notation, reverse polish notation;
Hope to adopt
Postfix natation Chinese meaning
You spelled it wrong. It should be Postfix notation.
It indicates a suffix expression"
A suffix expression requires that operators be placed behind two operation objects (operands. In a suffix expression, there is no operator priority problem, and there are no parentheses. The order of calculation is completely in the order in which the operator appears, which is much simpler than the value of the infix expression.
Generally, the compiler must convert the infix expression into a suffix expression before performing operations. For example, if the suffix Expression of c-a * B + d is cab *-d +, the suffix expression is scanned from left to right, and the first operator "*" is met. that is, the first two calculation objects are obtained for calculation (for example, a * B), the second operator "-", and the first two calculation objects are obtained for calculation (for example: c minus the result of a * B). When the third operator "+" is reached, the first two calculation results are obtained for calculation ...... Until the entire expression is completed.
Postfix Notation is also called Reverse Polish Notation.
It was named after the Polish mathematician Jan Lukasiewicz.
There is also a Prefix Notation that corresponds to a suffix expression, which is also called a Polish expression (Polish Notation ). In prefix expressions, operators appear before the two operation objects.