-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBaseBallHits.java
66 lines (55 loc) · 2.14 KB
/
BaseBallHits.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import java.util.*;
public class BaseBallHits {
public static void main(String args[]) {
// String[] input = {"5", "-2", "4", "Z", "X", "9", "+", "+"};
String[] input = {"1", "2", "+", "Z"};
int input_size = 4;
System.out.println(totalScore(input, input_size));
}
public static int totalScore(String[] blocks, int n) {
String BLOCK_X = "X";
String BLOCK_PLUS = "+";
String BLOCK_Z = "Z";
int blocks_len = blocks.length;
int prev_score_1 = 0;
int prev_score_2 = 0;
int total_score = 0;
for(int i = 0; i < n; i++) {
if(i < blocks_len) {
String current_block = blocks[i];
if(isInt(current_block)) {
int thisScore = Integer.parseInt(current_block);
total_score = total_score + thisScore;
prev_score_2 = prev_score_1;
prev_score_1 = thisScore;
} else if(BLOCK_X.equalsIgnoreCase(current_block)) {
int thisScore = prev_score_1 * 2;
total_score = total_score + thisScore;
prev_score_2 = prev_score_1;
prev_score_1 = thisScore;
} else if(BLOCK_PLUS.equalsIgnoreCase(current_block)) {
int thisScore = prev_score_1 + prev_score_2;
total_score = total_score + thisScore;
prev_score_2 = prev_score_1;
prev_score_1 = thisScore;
} else if(BLOCK_Z.equalsIgnoreCase(current_block)) {
int thisScore = -2;
total_score = total_score - prev_score_1;
prev_score_2 = prev_score_1;
prev_score_1 = thisScore;
}
} else {
return total_score;
}
}
return total_score;
}
public static boolean isInt(String intString) {
try{
Integer.parseInt(intString);
return true;
} catch (Exception e) {
return false;
}
}
}