682. Baseball Game

682. Baseball Game

class Solution {
			public int calPoints(String[] ops) {
		int currentSum = 0;
		ArrayList valid = new ArrayList();
		for (int i = 0; i < ops.length; i++) {

			if (ops[i].equals("C")) {
				currentSum -= new Integer(valid.get(valid.size() - 1));
				valid.remove(valid.size() - 1);
			} else if (ops[i].equals("D")) {
				int newScore = (2 * valid.get(valid.size() - 1));
				currentSum += newScore;
				valid.add(newScore);
			} else if (ops[i].equals("+")) {
				int newScore = new Integer(valid.get(valid.size() - 1)) + new Integer((valid.get(valid.size() - 2)));
				currentSum += newScore;
				valid.add(newScore);
			} else {
				valid.add(new Integer(ops[i]));
				currentSum += new Integer(ops[i]);
			}
		}
		return currentSum;

	}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s