-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBaseballGame.java
43 lines (37 loc) · 1.06 KB
/
BaseballGame.java
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
package com.smlnskgmail.jaman.leetcodejava.easy;
import java.util.Stack;
// https://leetcode.com/problems/baseball-game/
public class BaseballGame {
private final String[] input;
public BaseballGame(String[] input) {
this.input = input;
}
public int solution() {
Stack<Integer> stack = new Stack<>();
for (String op : input) {
switch (op) {
case "+":
int a = stack.pop();
int b = stack.pop();
stack.push(b);
stack.push(a);
stack.push(a + b);
break;
case "D":
stack.push(stack.peek() * 2);
break;
case "C":
stack.pop();
break;
default:
stack.push(Integer.valueOf(op));
break;
}
}
int result = 0;
for (int value : stack) {
result += value;
}
return result;
}
}