Java with ArrayList


  • 0
    H

    import java.util.ArrayList;
    public class calPoints{

    public static void main(String[] args) {
    String[] test1 = new String[]{ "5","2", "C", "D", "+"};
    System.out.println(calPoints(test1));
    }
    public static int calPoints(String[] ops) {
    int totalPoints = 0;
    ArrayList<Integer> arrlist = new ArrayList<Integer>(100);
    for (int i = 0; i< ops.length ; i++){
    if (ops[i].equals("C")){
    int currSize= arrlist.size();
    totalPoints -= arrlist.get(currSize-1);
    arrlist.remove(currSize-1);
    }else if (ops[i].equals("D")){
    int currSize= arrlist.size();
    totalPoints += (2* arrlist.get(currSize-1));
    arrlist.add(2* arrlist.get(currSize-1));
    }else if (ops[i].equals("+")){
    int currSize = arrlist.size();
    totalPoints += (arrlist.get(currSize-1) + arrlist.get(currSize-2));
    arrlist.add((arrlist.get(currSize-1) + arrlist.get(currSize-2)));
    }else{
    int temp = Integer.valueOf(ops[i]);
    arrlist.add(temp);
    totalPoints += temp;
    }
    }return totalPoints;
    }
    }


  • 0
    J

    Please use the built-in "```" format. It will make your code a lot more reader-friendly.


Log in to reply
 

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