File tree Expand file tree Collapse file tree 4 files changed +137
-0
lines changed
solution/0600-0699/0682.Baseball Game Expand file tree Collapse file tree 4 files changed +137
-0
lines changed Original file line number Diff line number Diff line change @@ -177,6 +177,55 @@ func calPoints(ops []string) int {
177
177
}
178
178
```
179
179
180
+ ### ** TypeScript**
181
+
182
+ ``` ts
183
+ function calPoints(ops : string []): number {
184
+ const stack = [];
185
+ for (const op of ops ) {
186
+ const n = stack .length ;
187
+ if (op === ' +' ) {
188
+ stack .push (stack [n - 1 ] + stack [n - 2 ]);
189
+ } else if (op === ' D' ) {
190
+ stack .push (stack [n - 1 ] * 2 );
191
+ } else if (op === ' C' ) {
192
+ stack .pop ();
193
+ } else {
194
+ stack .push (Number (op ));
195
+ }
196
+ }
197
+ return stack .reduce ((p , v ) => p + v );
198
+ }
199
+ ```
200
+
201
+ ### ** Rust**
202
+
203
+ ``` rust
204
+ impl Solution {
205
+ pub fn cal_points (ops : Vec <String >) -> i32 {
206
+ let mut stack = vec! [];
207
+ for op in ops {
208
+ match op . as_str () {
209
+ " +" => {
210
+ let n = stack . len ();
211
+ stack . push (stack [n - 1 ] + stack [n - 2 ]);
212
+ }
213
+ " D" => {
214
+ stack . push (stack . last (). unwrap () * 2 );
215
+ }
216
+ " C" => {
217
+ stack . pop ();
218
+ }
219
+ n => {
220
+ stack . push (n . parse :: <i32 >(). unwrap ());
221
+ }
222
+ }
223
+ }
224
+ stack . into_iter (). sum ()
225
+ }
226
+ }
227
+ ```
228
+
180
229
### ** ...**
181
230
182
231
```
Original file line number Diff line number Diff line change @@ -165,6 +165,55 @@ func calPoints(ops []string) int {
165
165
}
166
166
```
167
167
168
+ ### ** TypeScript**
169
+
170
+ ``` ts
171
+ function calPoints(ops : string []): number {
172
+ const stack = [];
173
+ for (const op of ops ) {
174
+ const n = stack .length ;
175
+ if (op === ' +' ) {
176
+ stack .push (stack [n - 1 ] + stack [n - 2 ]);
177
+ } else if (op === ' D' ) {
178
+ stack .push (stack [n - 1 ] * 2 );
179
+ } else if (op === ' C' ) {
180
+ stack .pop ();
181
+ } else {
182
+ stack .push (Number (op ));
183
+ }
184
+ }
185
+ return stack .reduce ((p , v ) => p + v );
186
+ }
187
+ ```
188
+
189
+ ### ** Rust**
190
+
191
+ ``` rust
192
+ impl Solution {
193
+ pub fn cal_points (ops : Vec <String >) -> i32 {
194
+ let mut stack = vec! [];
195
+ for op in ops {
196
+ match op . as_str () {
197
+ " +" => {
198
+ let n = stack . len ();
199
+ stack . push (stack [n - 1 ] + stack [n - 2 ]);
200
+ }
201
+ " D" => {
202
+ stack . push (stack . last (). unwrap () * 2 );
203
+ }
204
+ " C" => {
205
+ stack . pop ();
206
+ }
207
+ n => {
208
+ stack . push (n . parse :: <i32 >(). unwrap ());
209
+ }
210
+ }
211
+ }
212
+ stack . into_iter (). sum ()
213
+ }
214
+ }
215
+ ```
216
+
168
217
### ** ...**
169
218
170
219
```
Original file line number Diff line number Diff line change
1
+ impl Solution {
2
+ pub fn cal_points ( ops : Vec < String > ) -> i32 {
3
+ let mut stack = vec ! [ ] ;
4
+ for op in ops {
5
+ match op. as_str ( ) {
6
+ "+" => {
7
+ let n = stack. len ( ) ;
8
+ stack. push ( stack[ n - 1 ] + stack[ n - 2 ] ) ;
9
+ }
10
+ "D" => {
11
+ stack. push ( stack. last ( ) . unwrap ( ) * 2 ) ;
12
+ }
13
+ "C" => {
14
+ stack. pop ( ) ;
15
+ }
16
+ n => {
17
+ stack. push ( n. parse :: < i32 > ( ) . unwrap ( ) ) ;
18
+ }
19
+ }
20
+ }
21
+ stack. into_iter ( ) . sum ( )
22
+ }
23
+ }
Original file line number Diff line number Diff line change
1
+ function calPoints ( ops : string [ ] ) : number {
2
+ const stack = [ ] ;
3
+ for ( const op of ops ) {
4
+ const n = stack . length ;
5
+ if ( op === '+' ) {
6
+ stack . push ( stack [ n - 1 ] + stack [ n - 2 ] ) ;
7
+ } else if ( op === 'D' ) {
8
+ stack . push ( stack [ n - 1 ] * 2 ) ;
9
+ } else if ( op === 'C' ) {
10
+ stack . pop ( ) ;
11
+ } else {
12
+ stack . push ( Number ( op ) ) ;
13
+ }
14
+ }
15
+ return stack . reduce ( ( p , v ) => p + v ) ;
16
+ }
You can’t perform that action at this time.
0 commit comments