|
| 1 | +/** |
| 2 | + You are keeping the scores for a baseball game with strange rules. At the beginning of the game, you start with an empty record. |
| 3 | + |
| 4 | + You are given a list of strings operations, where operations[i] is the ith operation you must apply to the record and is one of the following: |
| 5 | + |
| 6 | + - An integer x. |
| 7 | + Record a new score of x. |
| 8 | + - '+'. |
| 9 | + Record a new score that is the sum of the previous two scores. |
| 10 | + - 'D'. |
| 11 | + Record a new score that is the double of the previous score. |
| 12 | + - 'C'. |
| 13 | + Invalidate the previous score, removing it from the record. |
| 14 | + |
| 15 | + Return the sum of all the scores on the record after applying all the operations. |
| 16 | + |
| 17 | + The test cases are generated such that the answer and all intermediate calculations fit in a 32-bit integer and that all operations are valid. |
| 18 | + |
| 19 | + |
| 20 | + Example 1: |
| 21 | + Input: ops = ["5","2","C","D","+"] |
| 22 | + Output: 30 |
| 23 | + Explanation: |
| 24 | + "5" - Add 5 to the record, record is now [5]. |
| 25 | + "2" - Add 2 to the record, record is now [5, 2]. |
| 26 | + "C" - Invalidate and remove the previous score, record is now [5]. |
| 27 | + "D" - Add 2 * 5 = 10 to the record, record is now [5, 10]. |
| 28 | + "+" - Add 5 + 10 = 15 to the record, record is now [5, 10, 15]. |
| 29 | + The total sum is 5 + 10 + 15 = 30. |
| 30 | + |
| 31 | + Example 2: |
| 32 | + Input: ops = ["5","-2","4","C","D","9","+","+"] |
| 33 | + Output: 27 |
| 34 | + Explanation: |
| 35 | + "5" - Add 5 to the record, record is now [5]. |
| 36 | + "-2" - Add -2 to the record, record is now [5, -2]. |
| 37 | + "4" - Add 4 to the record, record is now [5, -2, 4]. |
| 38 | + "C" - Invalidate and remove the previous score, record is now [5, -2]. |
| 39 | + "D" - Add 2 * -2 = -4 to the record, record is now [5, -2, -4]. |
| 40 | + "9" - Add 9 to the record, record is now [5, -2, -4, 9]. |
| 41 | + "+" - Add -4 + 9 = 5 to the record, record is now [5, -2, -4, 9, 5]. |
| 42 | + "+" - Add 9 + 5 = 14 to the record, record is now [5, -2, -4, 9, 5, 14]. |
| 43 | + The total sum is 5 + -2 + -4 + 9 + 5 + 14 = 27. |
| 44 | + |
| 45 | + Example 3: |
| 46 | + Input: ops = ["1","C"] |
| 47 | + Output: 0 |
| 48 | + Explanation: |
| 49 | + "1" - Add 1 to the record, record is now [1]. |
| 50 | + "C" - Invalidate and remove the previous score, record is now []. |
| 51 | + Since the record is empty, the total sum is 0. |
| 52 | + |
| 53 | + |
| 54 | + Constraints: |
| 55 | + - 1 <= operations.length <= 1000 |
| 56 | + - operations[i] is "C", "D", "+", or a string representing an integer in the range [-3 * 104, 3 * 104]. |
| 57 | + - For operation "+", there will always be at least two previous scores on the record. |
| 58 | + - For operations "C" and "D", there will always be at least one previous score on the record. |
| 59 | + */ |
| 60 | +class Solution { |
| 61 | + func calPoints(_ operations: [String]) -> Int { |
| 62 | + var numsArr = [Int]() |
| 63 | + for s in operations { |
| 64 | + if s == "C" { |
| 65 | + numsArr.popLast() |
| 66 | + } else if s == "D" { |
| 67 | + var newElement: Int = numsArr.last! |
| 68 | + newElement *= 2 |
| 69 | + numsArr.append(newElement) |
| 70 | + } else if s == "+" { |
| 71 | + let last = numsArr.last! |
| 72 | + let previousLast = numsArr[numsArr.count - 2] |
| 73 | + let newElement = last + previousLast |
| 74 | + numsArr.append(newElement) |
| 75 | + } else { |
| 76 | + numsArr.append(Int(s)!) |
| 77 | + } |
| 78 | + } |
| 79 | + return numsArr.reduce(0, +) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +let s = Solution() |
| 84 | +let r = s.calPoints(["5","2","C","D","+"]) |
| 85 | +print(r) |
0 commit comments