Skip to content

Commit 4abec7c

Browse files
committed
solve problem Basebell Game
1 parent 8f7dda9 commit 4abec7c

File tree

5 files changed

+63
-1
lines changed

5 files changed

+63
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ All solutions will be accepted!
3232
|485|[Max Consecutive Ones](https://leetcode-cn.com/problems/max-consecutive-ones/description/)|[java/py/js](./algorithms/MaxConsecutiveOnes)|Easy|
3333
|191|[Number Of 1 Bits](https://leetcode-cn.com/problems/number-of-1-bits/description/)|[java/py/js](./algorithms/NumberOf1Bits)|Easy|
3434
|258|[Add Digits](https://leetcode-cn.com/problems/add-digits/description/)|[java/py/js](./algorithms/AddDigits)|Easy|
35-
|520|[Detect Capital](https://leetcode-cn.com/problems/detect-capital/description/)|[java/py/js](./algorithms/DetectCaptial)|Easy|
35+
|520|[Detect Capital](https://leetcode-cn.com/problems/detect-capital/description/)|[java/py/js](./algorithms/DetectCaptial)|Easy|
36+
|682|[BaseBall Game](https://leetcode-cn.com/problems/baseball-game/description/)|[java/py/js](./algorithms/BaseBallGame)|Easy|

algorithms/BaseballGame/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# Baseball Game
2+
This problem is easy to solve

algorithms/BaseballGame/Solution.java

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int calPoints(String[] ops) {
3+
List<Integer> scoreStack = new ArrayList<Integer>();
4+
5+
for (String op : ops) {
6+
int size = scoreStack.size();
7+
if (op.equals("+")) {
8+
scoreStack.add(scoreStack.get(size - 2) + scoreStack.get(size - 1));
9+
} else if (op.equals("C")) {
10+
scoreStack.remove(size - 1);
11+
} else if (op.equals("D")) {
12+
scoreStack.add(2 * scoreStack.get(size - 1));
13+
} else {
14+
scoreStack.add(Integer.parseInt(op));
15+
}
16+
}
17+
int sum = 0;
18+
for (int i : scoreStack) {
19+
sum += i;
20+
}
21+
return sum;
22+
}
23+
}

algorithms/BaseballGame/solution.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {string[]} ops
3+
* @return {number}
4+
*/
5+
var calPoints = function(ops) {
6+
let scoreStack = []
7+
ops.forEach(op => {
8+
if (op === '+') {
9+
scoreStack.push(scoreStack[scoreStack.length - 2] + scoreStack[scoreStack.length - 1])
10+
} else if (op === 'C') {
11+
scoreStack.splice(scoreStack.length - 1, 1)
12+
} else if (op === 'D') {
13+
scoreStack.push(2 * scoreStack[scoreStack.length - 1])
14+
} else {
15+
scoreStack.push(parseInt(op))
16+
}
17+
})
18+
return scoreStack.reduce((x, y) => x + y, 0)
19+
};

algorithms/BaseballGame/solution.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution(object):
2+
def calPoints(self, ops):
3+
"""
4+
:type ops: List[str]
5+
:rtype: int
6+
"""
7+
score_stack = []
8+
for op in ops:
9+
if op not in ['+', 'C', 'D']:
10+
score_stack.append(int(op))
11+
elif op == '+':
12+
score_stack.append(score_stack[-2] + score_stack[-1])
13+
elif op == 'C':
14+
score_stack = score_stack[: -1]
15+
elif op == 'D':
16+
score_stack.append(score_stack[-1] * 2)
17+
return sum(score_stack)

0 commit comments

Comments
 (0)