class Solution {
public int calPoints(String[] ops) {
Stack stack = new Stack<>();
int top = 0;
int tmp = 0;
int sum = 0;
for(int i = 0 ;i<ops.length;i++){
switch(ops[i]){
case “+”:
top = stack.pop();
tmp = top+stack.peek();
stack.push(top);
sum+=stack.push(tmp);
break;
case"D":
tmp = stack.peek()*2;
sum += stack.push(tmp);
break;
case"C":
sum -= stack.pop();
break;
default:
stack.push(Integer.parseInt(ops[i]));
sum+=Integer.parseInt(ops[i]);
}
}
return sum;
}
}
你现在是一场采用特殊赛制棒球比赛的记录员。这场比赛由若干回合组成,过去几回合的得分可能会影响以后几回合的得分。 比赛开始时,记录是空白的。你会得到一个记录操作的字符串列表 ops,其中 ops[i]
最新推荐文章于 2024-06-09 15:03:53 发布