| 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 | }; |
nothing calls this directly
no outgoing calls
no test coverage detected