Evaluate Reverse Polish Notation
来源:互联网 发布:mac app下载 编辑:程序博客网 时间:2024/04/28 09:51
some example:
其实就是一个后序遍历求值的问题,利用栈的后进先出的思想,每次遇到一个运算符,进行两次出栈操作,找出对应的两个数字并计算值,然后重新入栈。以前一直以为只要判断字符<'0'或者>'9'就能判断是否是运算符,但是提交失败,原来会存在负数的情况,这点以前没有注意到,还好这里面只有4个运算符.
class Solution {public: int evalRPN(vector<string> &tokens) { stack<int> q; string str1[4]={"*","-","+","/"}; int flag = 0;for(vector<string>::const_iterator iter = tokens.begin();iter!=tokens.end();iter++){for(int i=0;i<(sizeof(str1)/sizeof(str1[1]));i++){ if(str1[i]==*iter) { flag=1; break;}}if(flag){int a=q.top();q.pop();int b=q.top();q.pop();int c;if((*iter)=="+"){c = a+b;}else if((*iter)=="-"){c =b-a;}else if((*iter)=="*"){c=a*b;}else if((*iter)=="/"){c = b/a;}q.push(c);flag =0;}elseq.push(atoi((*iter).c_str()));}int d =q.top();q.pop();return d; }};
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
- 如何让Div中的Table居中
- VC实现打开BMP
- android蓝牙传输文件时的文件格式限制及其解决方法
- java字符编码转换研究(转)
- Pro SharePoint with JQuery笔记1
- Evaluate Reverse Polish Notation
- c++按位操作符
- IOS开发Scoket接受报文不全(分多次接受)的TCP粘包问题
- android SearchView 样式修改
- ios设备唯一标识获取策略
- 一步步学习SPD2010--第十一章节--处理母版页(3)--母版页上使用的控件
- 如何让静态局部和全局变量或数组存在指定地址的Flash
- 黑马程序员___ 基础加强篇(二)
- Yii当中如何注册jQueryUI的js和CSS