[leetcode代码]Evaluate Reverse Polish Notation
来源:互联网 发布:python人工智能模块 编辑:程序博客网 时间:2024/06/06 04:13
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
我自己写了递归实现,发现巨2.
贴一下别人的代码吧。
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(); } };
0 0
- [leetcode代码]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
- Leetcode: 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
- 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
- 设计模式之享元模式
- nginx源码分析--nginx模块解析
- Android中ListView下拉刷新的实现
- 拨钟问题
- MFC中将所画图像保存到图像文件
- [leetcode代码]Evaluate Reverse Polish Notation
- oracle 创建表空间等
- 1142 成绩排序
- 分享一下世界名牌大学课件下载地址
- ant的使用
- 矩形转置
- Oracle索引分区杂谈
- 分治题集
- wimiy篮烦南捉呈谄灰荣