Evaluate 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.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9 ["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
解题思路:
后缀表达式计算的问题,做过计算机的同学应该都知道。遇到数字,进栈;遇到操作符,将前面两个操作数出栈,计算,将结果入栈。最后的结果会在栈中。
代码如下:
class Solution {
public:
int evalRPN(vector& tokens) {
stack s;
int len = tokens.size();
int num1, num2;
for(int i = 0; i
<!--
-->
原文作者:;如有侵权,请联系管理员删除。