-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0682-Baseball-game.cs
More file actions
50 lines (43 loc) · 1.25 KB
/
0682-Baseball-game.cs
File metadata and controls
50 lines (43 loc) · 1.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
namespace Solution._0682.Baseball_game
{
public class _0682_Baseball_game
{
public int CalPoints(string[] ops)
{
if (ops.Length == 0) return 0;
Stack<int> stack = new Stack<int>();
foreach (var s in ops)
{
switch (s)
{
case "D":
stack.Push(stack.Peek() * 2);
break;
case "C":
stack.Pop();
break;
case "+":
int x = stack.Pop();
int y = stack.Pop();
stack.Push(y);
stack.Push(x);
stack.Push(x + y);
break;
default:
stack.Push(int.Parse(s));
break;
}
}
int sum = 0;
while (stack.Count > 0)
sum += stack.Pop();
return sum;
// Or directly use Sum() of LINQ's extension method.
// return stack.Sum();
}
}
}