150. Evaluate Reverse Polish Notation
利用逆波兰表示法来模拟一个计算机的加减乘除结果,保证所有计算结果都
合法且最后一定会有解。
Example:
Input: tokens = ["10","6","9","3","+","-11","*","/","*","17","+","5","+"]
Output: 22
Explanation: ((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
思路:
1.用一个stack来处理运算式,如果遇到数字就push数字到stack。
2.如果遇到长度为1又不是数字的就从stack里面pop两个数字出来处理,并push回去。
3.返回stack里面剩下的元素。
Java Code: