LeetCode problem 2: Evaluate Reverse Polish Notation
来源:互联网 发布:中国社交网络使用排名 编辑:程序博客网 时间:2024/06/05 17:40
思路:碰到符号从数据栈中取两个数计算,再将计算结果压入数据栈中,最后得到的数据栈栈顶就是所求的结果。
class Solution {public: int evalRPN(vector<string> &tokens) { stack<int> num; int a,b,c; for(vector<string>::iterator it = tokens.begin();it != tokens.end();it++){ if("+" == *it || "*" == *it || "-" == *it || "/" == *it){ a = num.top(); num.pop(); b = num.top(); num.pop(); char str = (*it).at(0); switch(str){ case '+': c = a + b; break; case '-': c = b - a; break; case '*': c = a*b; break; case '/': c = b/a; break; } num.push(c); } else{ const char *p; int n; p = (*it).c_str(); n = atoi(p); num.push(n); } } a = num.top(); return a; }};
欢迎指出bug。
0 0
- LeetCode problem 2: Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation [2]
- LeetCode 2 Evaluate Reverse Polish Notation
- LeetCode 2 Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation【leetcode 2】
- 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
- 我对搜索算法的一点点理解
- Struts中的namespace
- 猜随机数
- archlinux 2014.03.01 硬盘安装 win + grub4dos + arch
- hdu 1114
- LeetCode problem 2: Evaluate Reverse Polish Notation
- 海量数据处理:十道面试题与十个海量数据处理方法总结
- SicilyOJ 11珠海赛重现 F.Greedy Snake(深搜)
- fcntl和flock两个系统调用的区别
- python读sqlserver库Grid显示窗体程序
- gentoo打补丁
- openwrt路由器的MTU设置问题
- 实例变量(instance var)与属性(@property)的关系
- MinGW编译db-db-4.8.30.NC.tar.gz遇到unexpected EOF while looking for matching‘''