Java || Using Stack || With Comments
\nclass Solution {\n    public int calPoints(String[] ops) {\n        if(ops.length ==0 || ops ==null)         // returning 0 if ops is null or empty\n         
```\nclass Solution {\n    public int calPoints(String[] ops) {\n        if(ops.length ==0 || ops ==null)         // returning 0 if ops is null or empty\n            return 0;\n        Stack<Integer> stack = new Stack();        \n        for(int i = 0; i<ops.length; i++) {            \n            if(ops[i].equals("D")) {                     //if "D" is encountered while traversing the string we push into stack 2*stack.peek() element.\n                stack.push(2*stack.peek());\n            }\n            else if(ops[i].equals("C")) {  //If "C" is encountered simply pop the element. No need to check for empty stack(its already handled as stated in the question).\n                stack.pop();\n            }\n            else if(ops[i].equals("+")) {       //The tricky part in my solution. if "+" is encountered in the ops array we pop out two elements add their sum and then again push the popped items one by one with thier sum.(Again no need to handle the empty case of stack already stated in the question).\n                int sum = 0;\n                int s1 = stack.pop();\n                int s2 = stack.pop();\n                sum = s1+s2;\n                stack.push(s2);\n                stack.push(s1);\n                stack.push(sum);\n            }\n            else {\n                stack.push(Integer.parseInt(ops[i]));                 // if the ith element is digit simply push it in the stack.\n            }\n        }\n        int sumOfStack = 0;\n        while(!stack.isEmpty()) {\n            sumOfStack+=stack.pop();                                //Finally pop all the items and return their sum\n        }\n        return sumOfStack;\n    }\n}\n```\n# 2 ms, faster than 81.46% of Java online submissions for Baseball Game.\n\n## Do upvote if you find the solution helpful.
['Stack', 'Java']