-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path682-BaseballGame.cs
32 lines (30 loc) · 1007 Bytes
/
682-BaseballGame.cs
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
//Problem: https://leetcode.com/problems/baseball-game
using System;
using System.Collections.Generic;
using System.Linq;
namespace LeetCode {
public partial class Solution {
public int CalPoints(string[] ops) {
var baseball = new Stack<int>();
for(int i=0; i < ops.Length; i++) {
if(ops[i] == "+") {
var last = baseball.Pop();
var previous = baseball.Peek();
baseball.Push(last);
baseball.Push(last + previous);
}
else if(ops[i] == "D") {
var peek = baseball.Peek();
baseball.Push(peek * 2);
}
else if(ops[i] == "C") {
baseball.Pop();
}
else {
baseball.Push(Convert.ToInt32(ops[i]));
}
}
return baseball.Sum();
}
}
}