Skip to content

Commit 49fff18

Browse files
committed
Create: 0682-baseball-game
1 parent d988919 commit 49fff18

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

csharp/0682-baseball-game.cs

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class Solution {
2+
public int CalPoints(string[] operations) {
3+
List<int> stack = new();
4+
5+
foreach(var operation in operations)
6+
{
7+
switch(operation)
8+
{
9+
case "D":
10+
stack.Add(stack[stack.Count()-1]*2);
11+
break;
12+
case "+":
13+
stack.Add(stack[stack.Count()-1] + stack[stack.Count()-2]);
14+
break;
15+
case "C":
16+
stack.RemoveAt(stack.Count()-1);
17+
break;
18+
default:
19+
stack.Add(int.Parse(operation));
20+
break;
21+
}
22+
}
23+
24+
return stack.Sum();
25+
}
26+
}

javascript/0682-baseball-game.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string[]} operations
3+
* @return {number}
4+
*/
5+
var calPoints = function (operations) {
6+
const stack = [];
7+
for(const op of operations){
8+
if(op==="+"){
9+
stack.push(stack[stack.length - 1] + stack[stack.length - 2]);
10+
}else if(op==="C"){
11+
stack.pop()
12+
}else if(op==="D"){
13+
stack.push(stack[stack.length - 1] * 2);
14+
}else{
15+
stack.push(parseInt(op))
16+
}
17+
}
18+
return stack.reduce((prev,curr)=>prev+curr,0)
19+
};

swift/0682-baseball-game.swift

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
func calPoints(_ operations: [String]) -> Int {
3+
var scoreArray = [Int]()
4+
for i in 0..<operations.count {
5+
if operations[i] == "C" {
6+
scoreArray.removeLast()
7+
} else if operations[i] == "D" {
8+
scoreArray.append(scoreArray.last! * 2)
9+
} else if operations[i] == "+" {
10+
scoreArray.append(scoreArray[scoreArray.count - 2] + scoreArray[scoreArray.count - 1])
11+
} else {
12+
scoreArray.append(Int(operations[i])!)
13+
}
14+
}
15+
return scoreArray.isEmpty ? 0 : scoreArray.reduce(0, +)
16+
}
17+
}

0 commit comments

Comments
 (0)