LeetCode Evaluate Reverse Polish Notation
2014-07-15 15:23
351 查看
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:
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<string> &tokens) { stack<int> myStack; vector<string>::iterator iter = tokens.begin(); for (; iter != tokens.end(); iter++) { if (*iter != "+" && *iter != "-" && *iter != "*" && *iter != "/") { int iot = atoi((*iter).c_str()); myStack.push(iot); } else{ int ia, ib; ib = myStack.top(); myStack.pop(); ia = myStack.top(); myStack.pop(); if (*iter == "+") { ia += ib; myStack.push(ia); } else if (*iter == "-") { ia -= ib; myStack.push(ia); } else if (*iter == "*") { ia *= ib; myStack.push(ia); } else if (*iter == "/") { ia /= ib; myStack.push(ia); } } } return myStack.top(); } };
相关文章推荐
- [LeetCode] Evaluate Reverse Polish Notation
- 每日AC - 后缀表达式计算结果-leetcode-evaluate-reverse-polish-notation
- LeetCode题解——Evaluate Reverse Polish Notation
- [leetcode]Evaluate Reverse Polish Notation
- leetcode--Evaluate Reverse Polish Notation
- Leetcode 150 Evaluate Reverse Polish Notation (求值逆波兰表达式)
- [leetcode]Evaluate Reverse Polish Notation
- [leetcode] 150.Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- 【Leetcode】Evaluate Reverse Polish Notation JAVA
- Evaluate Reverse Polish Notation(LeetCode)
- [LeetCode] Evaluate Reverse Polish Notation
- LeetCode:Evaluate Reverse Polish Notation
- 【LeetCode】Evaluate Reverse Polish Notation
- [Leetcode] Evaluate Reverse Polish Notation (Java)
- leetcode题解-150. Evaluate Reverse Polish Notation
- 【LeetCode】Evaluate Reverse Polish Notation
- LeetCode 2 Evaluate Reverse Polish Notation
- 【leetcode】Evaluate Reverse Polish Notation
- [LeetCode] 150. Evaluate Reverse Polish Notation