[LeetCode]Evaluate Reverse Polish Notation
来源:互联网 发布:腾讯软件无法连接网络 编辑:程序博客网 时间:2024/06/03 17:30
题目链接:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
本题计算逆波兰表达式的值,本身并不难不过在做的时候忽略了几个点:
1、计算输入的值可能是负数,
2、输入的表达式可能只有一个数字,
3、在计算除法的时候除数可能为0,不过貌似测试集中没有这种情况
/** * 计算逆波兰表达式的值 * 有效运算符为+、-、×、/ */int evalRPN(vector<string> &tokens) {int rs =0;stack<int>s;for(size_t i=0;i<tokens.size();++i){char c=tokens[i][0];if(isdigit(c)||isdigit(tokens[i][1])){const char * s_char = tokens[i].c_str();s.push(atoi(s_char));}else{int c1=s.top();s.pop();int c2=s.top();s.pop();int rt=0;//cout<<"c2 "<<c2<<" c1 "<<c<<" "<<c1<<endl;switch(c){case '+':rt=c2+c1;break;case '-':rt=c2-c1;break;case '*':rt=c2*c1;break;case '/':rt=c2/c1;break;}s.push(rt);//cout<<"rt "<<rt<<endl;}}return rs;}
0 0
- Evaluate Reverse Polish Notation | leetcode
- leetcode-Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode - Evaluate Reverse Polish Notation
- LeetCode | Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation (LeetCode)
- leetcode: Evaluate Reverse Polish Notation
- Leetcode Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation -- LeetCode
- 【LeetCode】Evaluate Reverse Polish Notation
- LeetCode-Evaluate Reverse Polish Notation
- LeetCode:Evaluate Reverse Polish Notation
- LeetCode Evaluate Reverse Polish Notation
- Leetcode:Evaluate Reverse Polish Notation
- codeforce 247 div.2 B
- TCP协议中FLAG的含义
- 思科大中华区副总裁殷康:云计算已进入Intercloud时代
- 虚拟机Ubuntu14不能上网
- 继承还是委托?
- [LeetCode]Evaluate Reverse Polish Notation
- 哎,暂时回不来了
- android PopupWindow实现从底部弹出或滑出选择菜单或窗口
- OpenGL ES2.0 – Iphone开发指引
- eclipse中配置c++开发环境 Eclipse + CDT + MinGW
- 记第一场Codeforces
- struts2 #parameters.XXX获取参数 在s:if标签中判断要注意!
- android 删除android应用下面未被引用到的资源
- MP4V2 ARM编译后,运行总是ABORT