Evaluate Reverse Polish Notation
2013-12-09 10:48
316 查看
依然很简单,就不多说了,注意库函数的使用和STL的使用就好。
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> tmp;
for (int i = 0; i < tokens.size(); ++i) {
if (tokens[i] != "+"
&& tokens[i] != "-"
&& tokens[i] != "*"
&& tokens[i] != "/") {
tmp.push(atoi(tokens[i].c_str()));
continue;
}
int a, b;
b = tmp.top();
tmp.pop();
a = tmp.top();
tmp.pop();
if (tokens[i] == "+")
tmp.push(a + b);
else if (tokens[i] == "-")
tmp.push(a - b);
else if (tokens[i] == "*")
tmp.push(a * b);
else
tmp.push(a / b);
}
return tmp.top();
}
};
http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> tmp;
for (int i = 0; i < tokens.size(); ++i) {
if (tokens[i] != "+"
&& tokens[i] != "-"
&& tokens[i] != "*"
&& tokens[i] != "/") {
tmp.push(atoi(tokens[i].c_str()));
continue;
}
int a, b;
b = tmp.top();
tmp.pop();
a = tmp.top();
tmp.pop();
if (tokens[i] == "+")
tmp.push(a + b);
else if (tokens[i] == "-")
tmp.push(a - b);
else if (tokens[i] == "*")
tmp.push(a * b);
else
tmp.push(a / b);
}
return tmp.top();
}
};
http://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
相关文章推荐
- Evaluate Reverse Polish Notation——LeetCode
- Evaluate Reverse Polish Notation
- LeetCode(150) Evaluate Reverse Polish Notation
- 【LeetCode-面试算法经典-Java实现】【150-Evaluate Reverse Polish Notation(计算逆波兰式)】
- Evaluate Reverse Polish Notation
- leetcode笔记:Evaluate Reverse Polish Notation(逆波兰式的计算)
- leetcode----Evaluate Reverse Polish Notation
- LeetCode- Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- 150. Evaluate Reverse Polish Notation
- EvaluateReversePolishNotation 逆波兰表达式
- No150-Evaluate Reverse Polish Notation
- 【leetcode】150. Evaluate Reverse Polish Notation
- 150.Evaluate Reverse Polish Notation(Stack-Medium)
- [LeetCode]Evaluate Reverse Polish Notation
- leetcode - Evaluate Reverse Polish Notation
- Leetcode:evaluate-reverse-polish-notation