Leetcode Evaluate Reverse Polish Notation
2024-09-26 04:02:11
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 此题是求解后缀表达式,题目比较简单,开一个数据stack即可,此题的改进版本是引入括号,此时要开一个数据stack和运算符stack
bool isOperator(string& op){
if(op == "+" || op == "-" || op == "*" || op == "/") return true;
else return false;
} int calc(int a, int b, char op){
switch(op){
case '+':return a+b;
case '-':return a-b;
case '*':return a*b;
case '/':return a/b;
}
} int evalRPN(vector<string> &tokens){
stack<int> num;
for(int i = ; i < tokens.size(); ++ i){
if(!isOperator(tokens[i])){
num.push(atoi(tokens[i].c_str()));
}else{
int num2 = num.top(); num.pop();
int num1 = num.top(); num.pop();
char op = tokens[i][];
num.push(calc(num1,num2,op));
}
}
return num.top();
}
最新文章
- Linux下的TeXlive 2015 中文问题
- 说一下output子句
- 指定YUM安装包的体系结构或版本
- GPL与LGPL的区别
- CSS让div背景透明
- window8左下角窗口和右上角窗口失效解决方法
- 在Linux上运行C#
- javascriptt切换组件MyTab.js封装
- 物理Data Guard主备切换步骤
- bugly集成了Tinker热更新
- Linux初学者必知的5个学习网站
- DataRead和DataSet的异同
- 10本Java架构师必读书籍
- Power BI Desktop 新年快乐!
- react中使用阿里Viser图表
- 安卓界面之Viewpager和Tablayout实现滑动界面
- cloudera cdh5.13.0 vmware 快速安装
- oracle忘记了sys,system 密码后怎么修改?
- yaf视图
- Android进程管理
热门文章
- mysql的存储过程
- (转)ORA-12519: TNS:no appropriate service handler found 的问题处理。
- Arch Linux Installation Guide
- java.lang.UnsupportedClassVersionError: org/xwiki/xxx : Unsupported major.minor version 51.0
- ORA-01041: 内部错误,hostdef 扩展名不存在
- hdu 4070 福州赛区网络赛J 贪心 ***
- C可变参数函数 实现
- map[C++]
- Shell拆分大文件
- APP设计师拿到APP产品原型开始,七步搞定APP设计(转)