题目链接:
思路:
模拟题,思路见代码即可。
代码:
java
class Solution {
public int calPoints(String[] operations) {
int[] sum = new int[1010];
int n = operations.length;
int idx= 0,res = 0;
//进行模拟
for(int i = 0;i<n;i++,idx++) {
if(operations[i].equals("+")) {
sum[idx] = sum[idx-1]+sum[idx-2];
}
else if(operations[i].equals("D")) {
sum[idx] = sum[idx-1]*2;
}
else if(operations[i].equals("C")) {
idx-=2;
}
else {
sum[idx] = Integer.parseInt(operations[i]);
}
}
for(int i =0;i<idx;i++)
res+=sum[i];
return res;
}
}