Evaluate Reverse Polish Notation
来源:互联网 发布:js push(obj) 编辑:程序博客网 时间:2024/06/05 14:54
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
计算逆波兰表达式
class Solution {public: int calc(int & l,int & r,const string & oper){ if(oper=="+") return l+r; if(oper=="-") return l-r; if(oper=="*") return l*r; if(oper=="/") return l/r; throw "wrong oper"; } int eval(vector<string>::const_reverse_iterator & ita){ const string & oper = *ita; if(oper == "+" || oper == "-" || oper == "*" || oper == "/"){ //运算符号 ++ita; int r = eval(ita); int l= eval(ita); return calc(l,r,oper); }else{ ++ita; return atoi(oper.c_str()); } } int evalRPN(vector<string> &tokens) { vector<string>::const_reverse_iterator ita = tokens.rbegin(); return eval(ita); }};
0 0
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- linker command failed with exit code 1 (use -v to see invocation)
- 妖哲的日常3 QA前线——讨伐!全自动页游
- 11-SVN服务器搭建和使用
- LingPipe's Competition
- RMAN SET NEWNAME
- Evaluate Reverse Polish Notation
- Linux下environ环境变量操作函数
- linux服务器如何检查远程用户登陆状态、时间、ip地址等信息
- Winform 多线程 文件操作
- iOS读取和显示PDF文档
- hdu 1846 (博弈)
- oracle-开窗函数-求最近7天平均值
- Nsis 宏定义及调用方法
- 调用系统相机的方式