File tree 2 files changed +34
-1
lines changed 2 files changed +34
-1
lines changed Original file line number Diff line number Diff line change
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
+ };
Original file line number Diff line number Diff line change @@ -156,7 +156,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
156
156
| 020 | [ Valid Parentheses] ( https://leetcode.com/problems/valid-parentheses/ ) | [ Python] ( ./Python/20_ValidParentheses.py ) | _ O(n)_ | _ O(n)_ | Easy | Stack | |
157
157
| 150 | [ Evaluate Reverse Polish Notation] ( https://leetcode.com/problems/evaluate-reverse-polish-notation/ ) | [ Python] ( ./Python/150.EvaluateReversePolishNotation.py ) | _ O(n)_ | _ O(1)_ | Medium | Stack | |
158
158
| 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 | |
160
160
161
161
<br />
162
162
<div align =" right " >
You can’t perform that action at this time.
0 commit comments