您的位置:首页 > 编程语言 > Lua

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/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: