|
| 1 | +/** |
| 2 | + * You're now a baseball game point recorder. |
| 3 | + * |
| 4 | + * Given a list of strings, each string can be one of the 4 following types: |
| 5 | + * |
| 6 | + * Integer (one round's score): Directly represents the number of points you get in this round. |
| 7 | + * "+" (one round's score): Represents that the points you get in this round are the sum of the last two valid round's points. |
| 8 | + * "D" (one round's score): Represents that the points you get in this round are the doubled data of the last valid round's points. |
| 9 | + * "C" (an operation, which isn't a round's score): Represents the last valid round's points you get were invalid and should be removed. |
| 10 | + * Each round's operation is permanent and could have an impact on the round before and the round after. |
| 11 | + * |
| 12 | + * You need to return the sum of the points you could get in all the rounds. |
| 13 | + * |
| 14 | + * Example 1: |
| 15 | + * Input: ["5","2","C","D","+"] |
| 16 | + * Output: 30 |
| 17 | + * Explanation: |
| 18 | + * Round 1: You could get 5 points. The sum is: 5. |
| 19 | + * Round 2: You could get 2 points. The sum is: 7. |
| 20 | + * Operation 1: The round 2's data was invalid. The sum is: 5. |
| 21 | + * Round 3: You could get 10 points (the round 2's data has been removed). The sum is: 15. |
| 22 | + * Round 4: You could get 5 + 10 = 15 points. The sum is: 30. |
| 23 | + * |
| 24 | + * Example 2: |
| 25 | + * Input: ["5","-2","4","C","D","9","+","+"] |
| 26 | + * Output: 27 |
| 27 | + * Explanation: |
| 28 | + * Round 1: You could get 5 points. The sum is: 5. |
| 29 | + * Round 2: You could get -2 points. The sum is: 3. |
| 30 | + * Round 3: You could get 4 points. The sum is: 7. |
| 31 | + * Operation 1: The round 3's data is invalid. The sum is: 3. |
| 32 | + * Round 4: You could get -4 points (the round 3's data has been removed). The sum is: -1. |
| 33 | + * Round 5: You could get 9 points. The sum is: 8. |
| 34 | + * Round 6: You could get -4 + 9 = 5 points. The sum is 13. |
| 35 | + * Round 7: You could get 9 + 5 = 14 points. The sum is 27. |
| 36 | + * |
| 37 | + * Note: |
| 38 | + * The size of the input list will be between 1 and 1000. |
| 39 | + * Every integer represented in the list will be between -30000 and 30000. |
| 40 | + */ |
| 41 | + |
| 42 | +public class BaseballGame682 { |
| 43 | + public int calPoints(String[] ops) { |
| 44 | + if (ops == null || ops.length == 0) return 0; |
| 45 | + Stack<Integer> stack = new Stack<>(); |
| 46 | + int sum = 0; |
| 47 | + for (String op: ops) { |
| 48 | + if (op.equals("+")) { |
| 49 | + if (stack.size() >= 2) { |
| 50 | + int e1 = stack.pop(); |
| 51 | + int e2 = stack.peek(); |
| 52 | + int twoSum = e1 + e2; |
| 53 | + sum += twoSum; |
| 54 | + stack.push(e1); |
| 55 | + stack.push(twoSum); |
| 56 | + } |
| 57 | + } else if (op.equals("D")) { |
| 58 | + if (!stack.isEmpty()) { |
| 59 | + int doubledScore = stack.peek() * 2; |
| 60 | + sum += doubledScore; |
| 61 | + stack.push(doubledScore); |
| 62 | + } |
| 63 | + } else if (op.equals("C")) { |
| 64 | + if (!stack.isEmpty()) { |
| 65 | + int lastScore = stack.pop(); |
| 66 | + sum -= lastScore; |
| 67 | + } |
| 68 | + } else { |
| 69 | + int now = Integer.parseInt(op); |
| 70 | + sum += now; |
| 71 | + stack.push(now); |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return sum; |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments