Code address: http://runjs.cn/code/r06uftvp
-
First of all, we have to figure out what reverse Polish expressions are. See: http://www.cnblogs.com/chenying99/p/3675876.html
-
Summarize roughly
-
Our usual calculation method, where operators are placed between two numbers is called an infix expression
-
eg 2 + 3 * (5 - 1)
-
-
An operator placed before two numbers is called a prefix expression, also called polish
-
eg + 2 * 3 - 5 1
-
From left to right until two numbers are encountered, use the operator before the two numbers to calculate
-
-
Operators placed after two numbers are called postfix expressions, also known as reverse Polish
-
eg 2 3 5 1 - * +
-
From right to left until two numbers are encountered, use the operator after the two numbers to calculate
-
-
-
According to the reverse Polish calculation method, we can draw some simple expression verification methods
-
The first two digits must be numbers
-
The number of operators can only be one less than the number of numbers
-
The last bit must be an operator
-
The length of the consecutive operator can only be one less than the preceding number (eg: 3 3 - - 3 3 -, which is incorrect)
-