题目链接:
https://leetcode.cn/problems/baseball-game/description/?envType=study-plan-v2&envId=programming-skills
思路:
模拟题,思路见代码即可。
代码:文章来源:https://www.toymoban.com/news/detail-821087.html
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;
}
}
文章来源地址https://www.toymoban.com/news/detail-821087.html
到了这里,关于leetcode08-棒球比赛的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!