Java | stack | one pass O(n) | simple and fast
Java stack solution with only one pass. I have simply maintained a variable (total) to prevent second pass.\n\npublic int calPoints(String[] ops) {\n\tStack<Int
Java stack solution with only one pass. I have simply maintained a variable (**total**) to prevent second pass.\n```\npublic int calPoints(String[] ops) {\n\tStack<Integer> s = new Stack<>();\n\tint total = 0, temp;\n\tfor(String op : ops) {           //  for each loop for string array\n\t\tif(op.equals("C")) {\n\t\t\ttotal -= s.pop();\n\t\t}\n\t\telse if(op.equals("D")) {\n\t\t\ttemp = 2*s.peek();\n\t\t\ts.push(temp);\n\t\t\ttotal += temp;\n\t\t}\n\t\telse if(op.equals("+")) {\n\t\t\ttemp = s.pop();\n\t\t\tint sum = temp + s.peek();\n\t\t\ts.push(temp);\n\t\t\ts.push(sum);\n\t\t\ttotal += sum;\n\t\t}\n\t\telse {\n\t\t\ttemp = Integer.parseInt(op);\n\t\t\ts.push(temp);\n\t\t\ttotal += temp;\n\t\t}\n\t}\n\treturn total;\n}\n```
['Java']