forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path682.cpp
More file actions
33 lines (32 loc) · 653 Bytes
/
682.cpp
File metadata and controls
33 lines (32 loc) · 653 Bytes
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
33
//Problem 682 : Baseball Game(using stack)
class Solution {
public:
int calPoints(vector<string>& ops)
{
int result = 0;
stack <int> score;
for (auto x : ops)
{
if (x == "C" && !score.empty())
score.pop();
else if(x == "D" && !score.empty())
score.push(2*score.top());
else if(x == "+" && !score.empty())
{
int top1 = score.top();
score.pop();
int top2 = score.top();
score.push(top1);
score.push((top1+top2));
}
else
score.push(stoi(x)); // convert from string to int then push on stack
}
while(!score.empty())
{
result += score.top();
score.pop();
}
return result;
}
};