来源:Leetcode
原帖:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
题目:
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
(http://en.wikipedia.org/wiki/Reverse_Polish_notation)
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
代码:
原帖:https://oj.leetcode.com/problems/evaluate-reverse-polish-notation/
题目:
Evaluate the value of an arithmetic expression in Reverse Polish Notation.
(http://en.wikipedia.org/wiki/Reverse_Polish_notation)
Valid operators are +, -, *, /. Each operand may be an integer or another expression.
Some examples:
["2", "1", "+", "3", "*"] -> ((2 + 1) * 3) -> 9
["4", "13", "5", "/", "+"] -> (4 + (13 / 5)) -> 6
代码:
class Solution {
public:
int evalRPN(vector<string> &tokens) {
stack<int> s;
for (int i = 0; i < tokens.size(); ++i) {
if (tokens[i] != "+" && tokens[i] != "-" && tokens[i] != "*" && tokens[i] != "/") {
s.push(stoi(tokens[i])); // string to int
} else {
int op2 = s.top();s.pop();
int op1 = s.top();s.pop();
int result = 0;
if (tokens[i] == "+") {
result = op1 + op2;
} else if (tokens[i] == "-") {
result = op1 - op2;
} else if (tokens[i] == "*") {
result = op1 * op2;
} else if (tokens[i] == "/") {
result = op1 / op2;
}
s.push(result);
}
}
return s.top();
}
};
No comments:
Post a Comment