682. Baseball Game
解析
简化版的中缀表达式转后缀表达式。
class Solution {
public:
int calPoints(vector<string>& ops) {
int sum=0;
int point=0;
int point2 = 0;
std::stack<int> Stack;
for (auto x : ops) {
if (x[0] =='-' || isdigit(x[0])) {
point = stoi(x);
sum += point;
Stack.push(point);
}
else if (x[0] == '+') {
point = Stack.top();
Stack.pop();
point2 = Stack.top();
Stack.pop();
sum += (point + point2);
Stack.push(point2);
Stack.push(point);
Stack.push(point + point2);
}
else if (x[0] == 'D') {
point = Stack.top() * 2;
Stack.pop();
sum += point;
Stack.push(point / 2);
Stack.push(point);
}
else if (x[0] == 'C') {
point = Stack.top();
Stack.pop();
sum -= point;
}
}
return sum;
}
};