leetcode.150. Evaluate Reverse Polish Notation
2016-05-10 20:47
477 查看
Evaluate the value of an arithmetic expression in
Reverse Polish Notation.
Valid operators are
Some examples:
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<string>& tokens) { int result = 0; int i; stack<int> opd; //存储操作数 int size = tokens.size(); for(i=0;i<size;i++) { if(tokens[i]=="*") { int rOpd = opd.top(); //右操作数 opd.pop(); int lOpd = opd.top(); //左操作数 opd.pop(); result = lOpd*rOpd; opd.push(result); } else if(tokens[i]=="/") { int rOpd = opd.top(); opd.pop(); int lOpd = opd.top(); opd.pop(); result = lOpd/rOpd; opd.push(result); } else if(tokens[i]=="+") { int rOpd = opd.top(); opd.pop(); int lOpd = opd.top(); opd.pop(); result = lOpd+rOpd; opd.push(result); } else if(tokens[i]=="-") { int rOpd = opd.top(); opd.pop(); int lOpd = opd.top(); opd.pop(); result = lOpd-rOpd; opd.push(result); } else { opd.push(atoi(tokens[i].c_str())); } } return opd.top(); } };
相关文章推荐
- Lua 垃圾收集机制
- 2dx-lua quick打包去掉蓝色启动页
- 150. Evaluate Reverse Polish Notation
- 2dx-lua精灵置灰
- [LeetCode]题解(python):150-Evaluate Reverse Polish Notation
- Lua和C++交互详细总结
- lua之alter提示框
- 实现c++,java,lua 在c++编写后需要在 pkg 文件中添加新的函数 然后批处理下生成GamePlatformToLua
- 在实现安卓和ios的sdk中 c++,java,lua 混编学习记录
- LuaDoc 3.0.1 Lua源代码文档生成工具(转)
- lua 随机数
- 基于ngx_lua_waf模块配置web应用防火墙
- 快速掌握Lua 5.3 —— 调试库 (2)
- 快速掌握Lua 5.3 —— 调试库 (1)
- 快速掌握Lua 5.3 —— userdata (2)
- 快速掌握Lua 5.3 —— userdata (1)
- lua类的继承
- lua基本实验
- lua 中的上n级模块路径函数分享
- Lua脚本语言简明入门教程