Skip to content

Commit 4cdc250

Browse files
authored
Added C++ code for stack problem Baseball Game (codedecks-in#95)
1 parent 3b70880 commit 4cdc250

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

C++/Baseball-Game.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Solution {
2+
public:
3+
int calPoints(vector<string>& ops) {
4+
stack<int> s;
5+
auto it=ops.begin();
6+
while(it!=ops.end()){
7+
if(*it=="+"){ //if char is + then new record is sum of last two records
8+
int val1=s.top();
9+
s.pop();
10+
int val2=s.top();
11+
s.push(val1);
12+
s.push(val1+val2);
13+
}
14+
else if(*it=="D"){ //if char is D then new record is twice the last record
15+
s.push(2*s.top());
16+
}
17+
else if(*it=="C"){ //if char is C then the last record is invalidated , hence popped
18+
s.pop();
19+
}
20+
else{ // if none of these conditions occur then just push the new record to stack
21+
s.push(stoi(*it));
22+
}
23+
it++;
24+
}
25+
int count=0;
26+
while(!s.empty()) //iteratively pop the top value of the stack and add it to the total
27+
{
28+
count+=s.top();
29+
s.pop();
30+
}
31+
return count;
32+
}
33+
};

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
156156
| 020 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Python](./Python/20_ValidParentheses.py) | _O(n)_ | _O(n)_ | Easy | Stack | |
157157
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Python](./Python/150.EvaluateReversePolishNotation.py) | _O(n)_ | _O(1)_ | Medium | Stack | |
158158
| 1047 | [Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string/) | [C++](./C++/remove-all-adjacent-duplicates-in-string.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | |
159-
159+
| 682 | [Baseball Game](https://leetcode.com/problems/baseball-game/) | [C++](./C++/Baseball-Game.cpp) | _O(n)_ | _O(n)_ | Easy | Stack | |
160160

161161
<br/>
162162
<div align="right">

0 commit comments

Comments
 (0)