# Leetcode: 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

int evalRPN(vector<string> &tokens) {
// IMPORTANT: Please reset any member data you declared, as
// the same Solution instance will be reused for each test case.
if(tokens.size()==1)return stoi(*tokens.begin());
stack<int> numbers;
vector<string>::iterator it = tokens.begin();
int op1 = 0;
int op2 = 0;
while(it != tokens.end())
{
if(*it == "+"||*it == "-" || *it == "*" || *it == "/")
{
op2 = numbers.top();
numbers.pop();
op1 = numbers.top();
numbers.pop();
if(*it == "+")
numbers.push(op1+op2);
else if(*it == "-")
numbers.push(op1-op2);
else if(*it == "*")
numbers.push(op1*op2);
else
numbers.push(op1/op2);
}
else
numbers.push(stoi(*it));
it++;
}
return numbers.top();
}

#### 【LeetCode】Evaluate Reverse Polish Notation 解题报告

2014-08-28 20:57:36

#### LeetCode 150 — Evaluate Reverse Polish Notation（C++ Java Python）

2014-03-01 15:34:39

#### 【LeetCode-面试算法经典-Java实现】【150-Evaluate Reverse Polish Notation（计算逆波兰式）】

2015-08-20 06:37:59

#### 【LeetCode刷题Java版】Evaluate Reverse Polish Notation（计算逆波兰表达式）

2014-10-13 18:02:40

#### Java Evaluate Reverse Polish Notation(逆波兰表达式)

2014-07-05 16:06:45

#### LeetCode | Evaluate Reverse Polish Notation

2013-12-17 11:22:41

#### Leetcode: Evaluate Reverse Polish Notation (Java)

2015-05-26 15:58:34

#### [leetcode-150]Evaluate Reverse Polish Notation(c++)

2015-08-20 15:25:45

#### Leetcode 150 Evaluate Reverse Polish Notation (求值逆波兰表达式）

2017-03-17 21:18:11

#### LeetCode150—Evaluate Reverse Polish Notation

2016-10-19 23:14:25