Beats 100% || Java || Arrays || Stack
Using Arrays(1ms)\n\n## Approach\n\n1. Array Initialization: Create an array scores to store the scores and an index to keep track of the current position in th
# Using Arrays(1ms)\n\n## Approach\n\n1. **Array Initialization**: Create an array `scores` to store the scores and an `index` to keep track of the current position in the array.\n2. **Switch Case**: Use a `switch` statement to handle the different operations (`"C"`, `"D"`, `"+"`, and default for numbers).\n   - `"C"`: Decrease the `index` to remove the last score.\n   - `"D"`: Double the last score and store it in the current position, then increase the `index`.\n   - `"+"`: Sum the last two scores and store it in the current position, then increase the `index`.\n   - Default: Convert the string to an integer and store it in the current position, then increase the `index`.\n3. **Sum Calculation**: Iterate through the scores array up to the `index` to calculate the sum of all the scores.\n\n\n# Code\n```\nclass Solution {\n    public int calPoints(String[] operations) {\n        int[] scores = new int[operations.length];\n        int index = 0;\n\n        for (String s : operations) {\n            switch (s) {\n                case "C":\n                    index--; // Remove the last score\n                    break;\n                case "D":\n                    scores[index] = scores[index - 1] * 2;\n                    index++;\n                    break;\n                case "+":\n                    scores[index] = scores[index - 1] + scores[index - 2];\n                    index++;\n                    break;\n                default:\n                    scores[index] = Integer.parseInt(s);\n                    index++;\n                    break;\n            }\n        }\n\n        int sum = 0;\n        for (int i = 0; i < index; i++) {\n            sum += scores[i];\n        }\n\n        return sum;\n    }\n}\n\n```\n\n---\n\n\n# Using Stack(2ms)\n\n# Approach\n\n1. **Iteration**: Use an enhanced `for` loop to iterate over the `operations` array.\n2. **Switch Case**: Use a `switch` statement for handling the different operations (`"C"`, `"D"`, `"+"`, and default for numbers).\n3. **Stack Operations**: For the `+` operation, store the last two popped values and then push back the values correctly.\n4. **Sum Calculation**: Iterate while the stack is not empty to calculate the sum of all the scores. \n\n```\n\nclass Solution {\n    public int calPoints(String[] operations) {\n        Stack<Integer> stk = new Stack<>();\n\n        for (String s : operations) {\n            switch (s) {\n                case "C":\n                    stk.pop();\n                    break;\n                case "D":\n                    stk.push(stk.peek() * 2);\n                    break;\n                case "+":\n                    int top1 = stk.pop();\n                    int top2 = stk.peek();\n                    stk.push(top1);\n                    stk.push(top1 + top2);\n                    break;\n                default:\n                    stk.push(Integer.parseInt(s));\n                    break;\n            }\n        }\n\n        int sum = 0;\n        while (!stk.isEmpty()) {\n            sum += stk.pop();\n        }\n\n        return sum;\n    }\n}\n```\n\n\n\n\n\n
['Array', 'Stack', 'Java']