package leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * https://leetcode.com/problems/baseball-game/
 */
public class Problem682 {
    public int calPoints(String[] ops) {
        int sum = 0;
        List<Integer> list = new ArrayList<>();
        int index = 0;
        for (int i = 0; i < ops.length; i++) {
            String op = ops[i];
            if (op.equals("D")) {
                int a = list.get(list.size() - 1) * 2;
                sum += a;
                list.add(sum);
            } else if (op.equals("C")) {
                sum -= Integer.parseInt(ops[index]);
                list.remove(list.size() - 1);
            } else if (op.equals("+")) {
                int a = list.get(list.size() - 1);
                int b = list.get(list.size() - 2);
                sum += (a + b);
                list.add(sum);
            } else {
                index = i;
                sum += Integer.parseInt(op);
                list.add(sum);
            }
        }
        return sum;
    }

    public static void main(String[] args) {
        Problem682 prob = new Problem682();
        System.out.println(prob.calPoints(new String[]{"5", "2", "C", "D", "+"})); // 15
        System.out.println(prob.calPoints(new String[]{"5", "-2", "4", "C", "D", "9", "+", "+"})); // 27
    }
}

@@ -260,31 +260,8 @@
 ();%0A
-        int index = 0;%0A
     
@@ -444,16 +444,19 @@
     
-sum += a
+list.add(a)
 ;%0A  
@@ -465,37 +465,32 @@
             
-list.add(sum)
+sum += a
 ;%0A          
@@ -540,42 +540,42 @@
     
-sum -= Integer.parseInt(ops%5Bindex%5D
+int val = list.get(list.size() - 1
 );%0A 
@@ -611,32 +611,60 @@
 st.size() - 1);%0A
+                sum -= val;%0A
             %7D el
@@ -806,23 +806,24 @@
         
-sum += 
+list.add
 (a + b);
@@ -835,36 +835,37 @@
             
-list.add(sum
+sum += (a + b
 );%0A         
@@ -896,17 +896,38 @@
     
-index = i
+list.add(Integer.parseInt(op))
 ;%0A  
@@ -985,360 +985,39 @@
     
-    list.add(sum);%0A            %7D%0A        %7D%0A        return sum;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem682 prob = new Problem682();%0A        System.out.println(prob.calPoints(new String%5B%5D%7B%225%22, %222%22, %22C%22, %22D%22, %22+%22%7D)); // 15%0A        System.out.println(prob.calPoints(new String%5B%5D%7B%225%22, %22-2%22, %224%22, %22C%22, %22D%22, %229%22, %22+%22, %22+%22%7D)); // 27
+%7D%0A        %7D%0A        return sum;
 %0A   

Solve problem 682