Skip to content

Commit 1ba2c7a

Browse files
authored
Create 682-baseball-game.js
Solved baseball-game in JS.
1 parent 3cfb9a5 commit 1ba2c7a

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

javascript/682-/682-baseball-game.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// problem link https://leetcode.com/problems/baseball-game/
2+
// time complexity O(n).
3+
4+
var calPoints = function(ops) {
5+
6+
const outputArr = [];
7+
8+
ops.forEach((op) => {
9+
10+
switch(op) {
11+
case 'D':
12+
outputArr.push(+outputArr[outputArr.length - 1] * 2);
13+
break;
14+
case "C":
15+
outputArr.pop();
16+
break;
17+
case "+":
18+
outputArr.push(+outputArr[outputArr.length - 1] + +outputArr[outputArr.length - 2]);
19+
break;
20+
default:
21+
outputArr.push(+op);
22+
}
23+
});
24+
25+
const result = outputArr.reduce((pre, currunt) => {
26+
return currunt + pre;
27+
},0);
28+
29+
return result;
30+
};

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy