LeetCode题解:Evaluate Reverse Polish Notation
2013-11-28 03:43
441 查看
Evaluate Reverse Polish Notation
Evaluate the value of an arithmetic expression inReverse 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
思路:
我真的不知道为什么LeetCode会出这么简单的题目。
题解:
class Solution { public: int evalRPN(vector<string> &tokens) { stack<int> numeric; for(auto& t : tokens) { if (isdigit(t[0]) || t.size()>1) numeric.push(atoi(t.c_str())); else { int o1, o2; o2 = numeric.top(); numeric.pop(); o1 = numeric.top(); numeric.pop(); switch(t[0]) { case '+': numeric.push(o1 + o2); break; case '-': numeric.push(o1 - o2); break; case '*': numeric.push(o1 * o2); break; case '/': numeric.push(o1 / o2); break; } } } return numeric.top(); } };
相关文章推荐
- leetcode题解-150. Evaluate Reverse Polish Notation
- leetcode_题解_Evaluate Reverse Polish Notation逆波兰式
- [LeetCode]题解(python):150-Evaluate Reverse Polish Notation
- LeetCode题解——Evaluate Reverse Polish Notation
- leetcode--Evaluate Reverse Polish Notation
- LeetCode – Refresh – Evaluate Reverse Polish Notation
- 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
- Leetcode: Evaluate Reverse Polish Notation
- evaluate-reverse-polish-notation(Leetcode)
- LeetCode OJ Evaluate Reverse Polish Notation
- LeetCode 150. Evaluate Reverse Polish Notation
- [Leetcode][JAVA] Evaluate Reverse Polish Notation
- 【Leetcode-medium-150】Evaluate Reverse Polish Notation
- leetcode--Evaluate Reverse Polish Notation
- 【LeetCode】-Evaluate Reverse Polish Notation
- LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium