Skip to content

Commit 9d76d68

Browse files
committed
Add Baseball Game
1 parent edf82c6 commit 9d76d68

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

LeetCode/682-BaseballGame.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
//Problem: https://leetcode.com/problems/baseball-game
2+
using System;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
6+
namespace LeetCode {
7+
public partial class Solution {
8+
public int CalPoints(string[] ops) {
9+
var baseball = new Stack<int>();
10+
for(int i=0; i < ops.Length; i++) {
11+
if(ops[i] == "+") {
12+
var last = baseball.Pop();
13+
var previous = baseball.Peek();
14+
baseball.Push(last);
15+
baseball.Push(last + previous);
16+
}
17+
else if(ops[i] == "D") {
18+
var peek = baseball.Peek();
19+
baseball.Push(peek * 2);
20+
}
21+
else if(ops[i] == "C") {
22+
baseball.Pop();
23+
}
24+
else {
25+
baseball.Push(Convert.ToInt32(ops[i]));
26+
}
27+
}
28+
29+
return baseball.Sum();
30+
}
31+
}
32+
}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ LeetCode C# solutions
55
|---| ----- | -------- | ---------- |
66
|**344**| **[Reverse String](https://leetcode.com/problems/reverse-string/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/344-ReverseString.cs)** | **Easy** |
77
|**509**| **[Fibonacci Number](https://leetcode.com/problems/fibonacci-number/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/509-FibonacciNumber.cs)** | **Easy** |
8+
|**682**| **[Fibonacci Number](https://leetcode.com/problems/baseball-game/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/682-BaseballGame.cs)** | **Easy** |
89
|**1432**| **[Kids With the Greatest Number of Candies](https://leetcode.com/problems/kids-with-the-greatest-number-of-candies/)** | **[C#](https://github.com/ocimen/leetcode/blob/main/LeetCode/1431-KidsWithGreatestNumberofCandies.cs)** | **Easy** |

0 commit comments

Comments
 (0)