@@ -66,7 +66,6 @@ The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
66
66
<li>For operations <code>"C"</code> and <code>"D"</code>, there will always be at least one previous score on the record.</li>
67
67
</ul >
68
68
69
-
70
69
## Solutions
71
70
72
71
<!-- tabs:start -->
@@ -76,45 +75,93 @@ The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27.
76
75
``` python
77
76
class Solution :
78
77
def calPoints (self , ops : List[str ]) -> int :
79
- stack = []
78
+ stk = []
80
79
for op in ops:
81
- if op == ' C ' :
82
- stack.pop( )
80
+ if op == ' + ' :
81
+ stk.append(stk[ - 1 ] + stk[ - 2 ] )
83
82
elif op == ' D' :
84
- stack .append(stack [- 1 ] << 1 )
85
- elif op == ' + ' :
86
- stack.append(stack[ - 1 ] + stack[ - 2 ] )
83
+ stk .append(stk [- 1 ] << 1 )
84
+ elif op == ' C ' :
85
+ stk.pop( )
87
86
else :
88
- stack .append(int (op))
89
- return sum (stack )
87
+ stk .append(int (op))
88
+ return sum (stk )
90
89
```
91
90
92
91
### ** Java**
93
92
94
93
``` java
95
94
class Solution {
96
95
public int calPoints (String [] ops ) {
97
- Deque<Integer > stack = new ArrayDeque<> ();
96
+ Deque<Integer > stk = new ArrayDeque<> ();
98
97
for (String op : ops) {
99
- if (" C" . equals(op)) {
100
- stack. pop();
98
+ if (" +" . equals(op)) {
99
+ int a = stk. pop();
100
+ int b = stk. peek();
101
+ stk. push(a);
102
+ stk. push(a + b);
101
103
} else if (" D" . equals(op)) {
102
- stack. push(stack. peek() << 1 );
103
- } else if (" +" . equals(op)) {
104
- Integer a = stack. pop();
105
- Integer b = stack. peek();
106
- stack. push(a);
107
- stack. push(a + b);
104
+ stk. push(stk. peek() << 1 );
105
+ } else if (" C" . equals(op)) {
106
+ stk. pop();
108
107
} else {
109
- stack . push(Integer . valueOf(op));
108
+ stk . push(Integer . valueOf(op));
110
109
}
111
110
}
112
- int res = 0 ;
113
- for (Integer score : stack) {
114
- res += score;
111
+ return stk. stream(). mapToInt(Integer :: intValue). sum();
112
+ }
113
+ }
114
+ ```
115
+
116
+ ### ** C++**
117
+
118
+ ``` cpp
119
+ class Solution {
120
+ public:
121
+ int calPoints(vector<string >& ops) {
122
+ vector<int > stk;
123
+ for (auto& op : ops)
124
+ {
125
+ int n = stk.size();
126
+ if (op == "+")
127
+ {
128
+ int a = stk[ n - 1] ;
129
+ int b = stk[ n - 2] ;
130
+ stk.push_back(a + b);
131
+ }
132
+ else if (op == "D") stk.push_back(stk[ n - 1] * 2);
133
+ else if (op == "C") stk.pop_back();
134
+ else stk.push_back(stoi(op));
115
135
}
116
- return res ;
136
+ return accumulate(stk.begin(), stk.end(), 0) ;
117
137
}
138
+ };
139
+ ```
140
+
141
+ ### **Go**
142
+
143
+ ```go
144
+ func calPoints(ops []string) int {
145
+ var stk []int
146
+ for _, op := range ops {
147
+ n := len(stk)
148
+ switch op {
149
+ case "+":
150
+ stk = append(stk, stk[n-1]+stk[n-2])
151
+ case "D":
152
+ stk = append(stk, stk[n-1]*2)
153
+ case "C":
154
+ stk = stk[:n-1]
155
+ default:
156
+ num, _ := strconv.Atoi(op)
157
+ stk = append(stk, num)
158
+ }
159
+ }
160
+ ans := 0
161
+ for _, score := range stk {
162
+ ans += score
163
+ }
164
+ return ans
118
165
}
119
166
```
120
167
0 commit comments