MCPcopy Create free account
hub / github.com/codedecks-in/LeetCode-Solutions / Solution

Class Solution

C++/Baseball-Game.cpp:1–33  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1class Solution {
2public:
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};

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected