-
Notifications
You must be signed in to change notification settings - Fork 28
/
Baseball_Game.cpp
32 lines (32 loc) · 930 Bytes
/
Baseball_Game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Solution {
public:
int calPoints(vector<string>& ops) {
stack<int> Stack;
int sum = 0;
for(int i = 0; i < ops.size(); i++) {
if(ops[i] == "+") {
int a = Stack.top();
Stack.pop();
int b = Stack.top();
Stack.pop();
int c = a + b;
Stack.push(b); Stack.push(a); Stack.push(c);
sum += c;
} else if(ops[i] == "D") {
int top = Stack.top();
top <<= 1;
sum += top;
Stack.push(top);
} else if(ops[i] == "C") {
int top = Stack.top();
Stack.pop();
sum -= top;
} else {
int value = atoi(ops[i].c_str());
sum += value;
Stack.push(value);
}
}
return sum;
}
};