[LeetCode]Evaluate Reverse Polish Notation
2014-04-06 10:26
309 查看
这道题主要难点是栈(stack)的使用。
#include <iostream> #include <string.h> #include <vector> #include <stack> using namespace std; class Solution { public: bool IsOp(string s) { if (s == "+" || s == "-" || s == "*" || s == "/") return true; else return false; } int evalRPN(vector<string> &tokens) { vector<string>::iterator it; stack<int> s; int result = 0; for (it = tokens.begin(); it != tokens.end(); it++) { int a; if (!IsOp(*it)) { a = atoi((*it).data()); s.push(a); } else { char op = (*it).data()[0]; int a = s.top(); s.pop(); int b = s.top(); s.pop(); switch (op) { case '+':result = b + a; break; case '-':result = b - a; break; case '*':result = b * a; break; case '/':result = b / a; break; default: break; } s.push(result); } } return s.top(); } };
相关文章推荐
- Leetcode 150: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
- LeetCode Reverse Words in a String & 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
- [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 (Java)
- LeetCode150:Evaluate Reverse Polish Notation
- [Leetcode] evaluate reverse polish notation 计算逆波兰表达式