Using ArrayList in JAVA || Well Explained✅
\n\n# Approach\nThe approach of the given code is to calculate the total score in a game of baseball, given an array of operations (\'ops\'). The code uses an A
\n\n# Approach\nThe approach of the given code is to calculate the total score in a game of baseball, given an array of operations (\'ops\'). The code uses an ArrayList \'list\' to keep track of the scores at each round.\n\n1. Initialization: The code initializes the integer \'ans\' to 0 and the ArrayList \'list\' to an empty list.\n\n2. Iterating through the operations: The code iterates through the \'ops\' array using a for loop.\n- If the current element in the \'ops\' array is "+", it adds the sum of the last two elements in the \'list\' to the \'list\'.\n- If the current element in the \'ops\' array is "D", it adds double the last element in the \'list\' to the \'list\'.\n- If the current element in the \'ops\' array is "C", it removes the last element from the \'list\'.\n- If the current element in the \'ops\' array is a number, it converts it to an integer and adds it to the \'list\'.\n\n3. Calculating the total score: The code iterates through the \'list\' and adds up all the elements in the \'list\' to get the total score, which is stored in \'ans\'.\n\n\n\n\n\n\n# Complexity\n- Time complexity:\nThe time complexity of the code is O(n), where n is the number of elements in the \'ops\' array. This is because the code only iterates through the \'ops\' array once and performs a constant amount of operations for each element in the array.\n\n- Space complexity:\nThe space complexity of the code is O(n), where n is the number of elements in the \'ops\' array. This is because the code uses an ArrayList \'list\' to store the scores of each round, which has a maximum size of n.\n\n# Note -\nThe difference between \'==\' and \'.equals()\' in Java is as follows:\n\n- \'==\' operator: Compares if two references refer to the same object in memory.\n- \'.equals()\' method: Compares the values of two objects.\n\nIn the given code, the elements in the \'ops\' array are strings. When you compare strings, you need to use the \'.equals()\' method, not the \'==\' operator, as the latter only checks if two references point to the same object in memory. The \'.equals()\' method, on the other hand, compares the actual contents of the strings and returns true if the contents are equal, regardless of whether the strings are stored in separate memory locations or not.\n\n\n# Code\n```\nclass Solution {\n    public int calPoints(String[] ops) {\n        int ans = 0;                                  \n        ArrayList<Integer> list = new ArrayList<>();\n        for(int i=0;i<ops.length;i++){\n    \n            if(ops[i].equals("+")){\n                int sum = list.get(list.size()-1)+list.get(list.size()-2);\n                list.add(sum);\n            }\n            else if(ops[i].equals("D")){\n                int prod = 2*list.get(list.size()-1);\n                list.add(prod);\n            }\n            else if(ops[i].equals("C")){\n                list.remove(list.size()-1);\n            }\n            else{\n                int t = Integer.parseInt(ops[i]);\n                list.add(t);\n            }\n        }\n        for(int i=0;i<list.size();i++){\n            ans +=list.get(i);\n        }\n        return ans;\n    }\n}\n```\n![upvote.jpeg](https://assets.leetcode.com/users/images/88b84055-0606-4da3-be81-af34aec51353_1675707585.5151799.jpeg)\n
['Java']