-
Notifications
You must be signed in to change notification settings - Fork 1
/
_0682_Baseball_Game.java
45 lines (40 loc) · 1.05 KB
/
_0682_Baseball_Game.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
44
// https://leetcode.com/problems/baseball-game/
import java.util.*;
public class _0682_Baseball_Game {
public static void main(String[] args) {
String[] ops = {"5","-2","4","C","D","9","+","+"};
System.out.println(calPoints(ops));
}
public static int calPoints(String[] ops)
{
int score = 0;
Stack<Integer> check = new Stack<>();
for(String str : ops)
{
if(str.equals("C"))
{
check.pop();
}
else if(str.equals("D"))
{
check.push(2*check.peek());
}
else if(str.equals("+"))
{
int temp = check.pop();
int temp2 = check.peek();
check.push(temp);
check.push(temp + temp2);
}
else
{
check.push(Integer.parseInt(str));
}
}
while(!check.isEmpty())
{
score += check.pop();
}
return score;
}
}