My Accepted Code in Java 7ms


  • 2
    D

    My idea is that just keeping track of the current valid round's point in an array and use it to have a next valid round's point.

      public int calPoints(String[] ops) {
        int sum = 0;
        int valid[] = new int[ops.length];
        int v = -1;
        for (int i = 0; i < ops.length; i++) {
          if (ops[i].equals("C")) {
            sum -= valid[v];
            v--;
          } else if (ops[i].equals("D")) {
            int d = (valid[v] + valid[v]);
            v++;
            valid[v] = d;
            sum += d;
          } else if (ops[i].equals("+")) {
            int p = (valid[v] + valid[v - 1]);
            v++;
            valid[v] = p;
            sum += p;
          } else {
            int val = Integer.parseInt(ops[i]);
            v++;
            valid[v] = val;
            sum += val;
          }
        }
    
        return sum;
      }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.