Evaluate Reverse Polish Notation
来源:互联网 发布:iphone5s联通4g网络 编辑:程序博客网 时间:2024/05/01 10:12
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 evalRPN(vector<string> &tokens) { stack< int> stk; for( int i = 0; i < tokens.size(); ++i){ if( tokens[i] != "+" && tokens[i] != "-" && tokens[i] != "/" && tokens[i] != "*"){ stk.push(atoi( tokens[i].c_str())); } else{ int left = 0, right = 0; if( !stk.empty()){ right = stk.top(); stk.pop(); } else return 0; if( !stk.empty()){ left = stk.top(); stk.pop(); } if( tokens[i] == "+") stk.push( left + right); else if( tokens[i] == "-") stk.push( left - right); else if( tokens[i] == "/") stk.push( left / right); else if( tokens[i] == "*") stk.push( left * right); else return 0; } } return stk.top(); }};
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
- iframe自适应内容高度的处理方法
- 大数类
- 第八十八题(金山笔试题,字符串移动)
- word插入图片显示不完整的解决办法
- MTK Android Driver :sound
- Evaluate Reverse Polish Notation
- 关于JAVA中事件分发和监听机制实现的代码实例-绝对原创实用
- 今日作息及食谱(7.5)
- 被卡住的进度条
- Android中覆盖
- 最长上升子序列
- centos 安装 pcre
- 苹果
- 从服务器更新团队项目后pom报错(用Maven管理)的解决方案