import java.util.ArrayList;
//leetcode submit region begin(Prohibit modification and deletion)
class Solution {
public int calPoints(String[] ops) {
int ret = 0;
ArrayList<Integer> scores = new ArrayList<>();
for (String op : ops) {
int n = scores.size();
switch (op.charAt(0)) {
case '+':
ret += scores.get(n - 1) + scores.get(n - 2);
scores.add(scores.get(n - 1) + scores.get(n - 2));
break;
case 'C':
ret -= scores.get(n - 1);
scores.remove(n - 1);
break;
case 'D':
ret += 2 * scores.get(n - 1);
scores.add(2 * scores.get(n - 1));
break;
default:
ret += Integer.parseInt(op);
scores.add(Integer.parseInt(op));
}
}
return ret;
}
}
//leetcode submit region end(Prohibit modification and deletion)
【leetcode 棒球比赛】
最新推荐文章于 2024-08-15 05:02:54 发布