LeetCode problem 2: Evaluate Reverse Polish Notation
2014-03-30 22:09
344 查看
思路:碰到符号从数据栈中取两个数计算,再将计算结果压入数据栈中,最后得到的数据栈栈顶就是所求的结果。
欢迎指出bug。
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。
相关文章推荐
- leetcode_150 Evaluate Reverse Polish Notation
- LeetCode: 150_Evaluate Reverse Polish Notation | 分析逆波兰式 | Medium
- LeetCode: Reverse Words in a String:Evaluate Reverse Polish Notation
- [LeetCode] Evaluate Reverse Polish Notation [2]
- Leetcode: Evaluate Reverse Polish Notation
- LeetCode-Evaluate Reverse Polish Notation
- [LeetCode]Evaluate Reverse Polish Notation(逆波兰式的计算)
- LeetCode(150) Evaluate Reverse Polish Notation
- leetcode第一刷_Evaluate Reverse Polish Notation
- Leetcode: Evaluate Reverse Polish Notation (Java)
- 【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 [150]
- Leetcode 150 Evaluate Reverse Polish Notation (求值逆波兰表达式)
- LeetCode150:Evaluate Reverse Polish Notation
- [Leetcode] Evaluate Reverse Polish Notation (Java)
- leetcode Evaluate Reverse Polish Notation(*)
- LeetCode -- Evaluate Reverse Polish Notation