LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation

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 地址:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/

计算波兰后缀表达式的值,具体做法是利用一个栈,如果遇到数字则把数字进栈,如果遇到操作符就把栈中的数字出栈进行运算(二元操作符出栈两个数字,一元操作符出栈一个数字),最后的结果存在栈中。代码:
 class Solution {
public:
int evalRPN(vector<string> &tokens) {
int lop, rop;
vector<string>::const_iterator cIter = tokens.begin();
stack<int> s;
for(; cIter != tokens.end(); ++cIter){
if ((*cIter).size() == && !isdigit((*cIter)[])){
char op = (*cIter)[];
rop = s.top();
s.pop();
lop = s.top();
s.pop();
if ('+' == op){
s.push(lop + rop);
} else if ('-' == op){
s.push(lop - rop);
} else if ('*' == op){
s.push(lop * rop);
} else{
s.push(lop / rop);
}
} else {
s.push(atoi((*cIter).c_str()));
}
}
return s.top();
}
};

最新文章

  1. linux内核分析作业8:理解进程调度时机跟踪分析进程调度与进程切换的过程
  2. 【转】How to hire——创业公司应该如何招人
  3. javascript数据结构与算法---栈
  4. python 学习 : 一个简单的秒表
  5. jquery 之ajax获取数据
  6. Cocos2d-x MultipleTouch &amp; CCControllButton&#39;s confusion
  7. vc2010配置opencv2.4.4库(图文 转)
  8. bootstrap table笔记
  9. java代码如何发送QQ邮件
  10. input是否checked与使用jquery的attr或prop方法无关
  11. Node.js学习笔记(三): 事件机制
  12. Ubuntu 下安装 matlab2018a
  13. tensorflow GPU版本安装及配置
  14. mysql开发总结
  15. sql server 清理缓存
  16. html 绘制矩形轨迹,选中区域
  17. I.MX6 OTG set as slave device hacking
  18. java 多线程1:进程与线程概述
  19. 温故而知新-XML和WEB服务器
  20. css,js移动资源

热门文章

  1. (转载) VS编译duilib项目时候的错误解决方法整理
  2. uilib库gdi句柄泄漏bug修复,duilib防止gdi泄漏的小提醒
  3. [LeetCode] Container With Most Water 简要分析
  4. POI 读取Excel文档中的数据——兼容Excel2003和Excel2007
  5. login:用户登陆的意思
  6. 关于FireFox下 CSS3 transition 与其他浏览器的差异
  7. mybatis系列-07-输出映射
  8. BITED数学建模七日谈之二:怎样阅读数学模型教材
  9. openstack deug
  10. DAG的生成